Github Cheatsheet
Github Cheatsheet
GitHub is a powerful platform for version control, collaboration, CI/CD automation, and DevOps workflows. This cheatsheet provides an in-depth guide to using GitHub, covering basic operations to advanced features.
1. Introduction to GitHub
What is GitHub?
GitHub is a web-based platform that uses Git for version control and provides tools for:
- Collaborative software development
- CI/CD automation
- Project management
- Code review and DevOps integration
Key Features
- Git Repositories: Centralized code hosting with Git.
- Collaboration: Pull requests, code reviews, and discussions.
- Actions: Automate workflows with GitHub Actions.
- Project Management: Boards, issues, and milestones for agile workflows.
- Security: Dependabot alerts and code scanning for vulnerabilities.
2. Getting Started
Creating an Account
- Sign up at GitHub.
- Create or join an organization for team collaboration.
Adding SSH Keys
-
Generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-
Add the key to your GitHub account:
- Go to Settings → SSH and GPG keys → Add Key.
Creating a Repository
- Go to Repositories → New.
- Configure repository name, description, and visibility.
- Add a
.gitignore
file or license if needed.
3. Basic GitHub Operations
Cloning a Repository
git clone git@github.com:username/repository.git
Committing and Pushing Changes
# Stage changes git add . # Commit changes git commit -m "Initial commit" # Push changes git push origin main
Pulling Changes
git pull origin main
4. Branching and Merging
Creating and Switching Branches
# Create a new branch git checkout -b feature-branch # Switch to an existing branch git checkout main
Pushing a Branch
git push origin feature-branch
Merging Branches
- Open a Pull Request on GitHub:
- Navigate to the repository → Pull Requests → New Pull Request.
- Review and merge changes.
Deleting a Branch
# Delete locally git branch -d feature-branch # Delete on remote git push origin --delete feature-branch
5. GitHub Issues and Project Boards
Creating an Issue
- Go to Issues → New Issue.
- Add title, description, and assign labels or assignees.
Automating Project Boards
- Add Issues Automatically:
- Go to the project board.
- Set up automation rules like "Add issues in progress."
Linking Pull Requests to Issues
Use keywords in PR descriptions:
Fixes #issue_number Closes #issue_number
6. GitHub Actions (CI/CD)
GitHub Actions is a workflow automation tool for CI/CD.
Basics of .github/workflows/<workflow>.yml
Example Workflow:
name: CI Pipeline on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout Code uses: actions/checkout@v3 - name: Install Dependencies run: npm install - name: Run Tests run: npm test
Workflow Triggers
- push: Runs the workflow when a commit is pushed.
- pull_request: Triggers on pull requests.
- schedule: Triggers on a cron schedule.
Managing Secrets
- Go to Settings → Secrets and variables → Actions.
- Add variables like
AWS_ACCESS_KEY_ID
orDOCKER_PASSWORD
.
Example with Secrets
jobs: deploy: runs-on: ubuntu-latest steps: - name: Deploy to AWS run: aws s3 sync ./build s3://my-bucket env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
7. GitHub Packages
Using GitHub as a Docker Registry
-
Authenticate:
docker login ghcr.io -u USERNAME -p TOKEN
-
Build and Push:
docker build -t ghcr.io/username/image-name:tag . docker push ghcr.io/username/image-name:tag
Installing from GitHub Packages
-
Add dependency in
package.json
(Node.js):"dependencies": { "package-name": "github:username/repository" }
8. Advanced GitHub Features
Protecting Branches
- Go to Settings → Branches.
- Enable branch protection rules (e.g., prevent force-pushes, require PR reviews).
Code Review Automation
- Use GitHub Apps like CodeCov or LGTM for automated code review.
Dependency Management with Dependabot
- Enable Dependabot under Insights → Dependency Graph.
- Dependabot creates pull requests to update outdated dependencies.
9. GitHub Security
Code Scanning
-
Enable Code Scanning Alerts under Security.
-
Include scanning actions in workflows:
- name: CodeQL Analysis uses: github/codeql-action/analyze@v2
Secret Scanning
- GitHub scans public repositories for leaked secrets and alerts the repository owner.
Enabling 2FA
- Go to Settings → Account Security → Enable Two-Factor Authentication.
10. GitHub CLI (gh)
Installing GitHub CLI
brew install gh # macOS sudo apt install gh # Linux
Authenticating
gh auth login
Common Commands
-
Clone a Repository:
gh repo clone username/repository
-
Create a Pull Request:
gh pr create --title "Feature Update" --body "Details of PR"
-
List Issues:
gh issue list
11. GitHub API
Using the API
Authenticate using a personal access token:
curl -H "Authorization: token YOUR_TOKEN" https://api.github.com/user/repos
Example: Creating an Issue
curl -X POST -H "Authorization: token YOUR_TOKEN" \ -H "Content-Type: application/json" \ -d '{"title": "Bug Report", "body": "Description of the bug"}' \ https://api.github.com/repos/username/repository/issues
12. GitHub Best Practices
-
Use Descriptive Commit Messages:
Fix bug in login page #123
-
Enable Branch Protections to enforce review processes.
-
Automate Testing using GitHub Actions for pull requests.
-
Use Issues and Labels for effective project tracking.
References and Resources
Follow me on : Medium Linkedin Researchgate