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.
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
- 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.
- Sign up at GitHub.
- Create or join an organization for team collaboration.
-
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.
- Go to Repositories → New.
- Configure repository name, description, and visibility.
- Add a
.gitignorefile or license if needed.
git clone git@github.com:username/repository.git# Stage changes
git add .
# Commit changes
git commit -m "Initial commit"
# Push changes
git push origin maingit pull origin main# Create a new branch
git checkout -b feature-branch
# Switch to an existing branch
git checkout maingit push origin feature-branch- Open a Pull Request on GitHub:
- Navigate to the repository → Pull Requests → New Pull Request.
- Review and merge changes.
# Delete locally
git branch -d feature-branch
# Delete on remote
git push origin --delete feature-branch- Go to Issues → New Issue.
- Add title, description, and assign labels or assignees.
- Add Issues Automatically:
- Go to the project board.
- Set up automation rules like "Add issues in progress."
Use keywords in PR descriptions:
Fixes #issue_number
Closes #issue_number
GitHub Actions is a workflow automation tool for CI/CD.
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- push: Runs the workflow when a commit is pushed.
- pull_request: Triggers on pull requests.
- schedule: Triggers on a cron schedule.
- Go to Settings → Secrets and variables → Actions.
- Add variables like
AWS_ACCESS_KEY_IDorDOCKER_PASSWORD.
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 }}-
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
-
Add dependency in
package.json(Node.js):"dependencies": { "package-name": "github:username/repository" }
- Go to Settings → Branches.
- Enable branch protection rules (e.g., prevent force-pushes, require PR reviews).
- Use GitHub Apps like CodeCov or LGTM for automated code review.
- Enable Dependabot under Insights → Dependency Graph.
- Dependabot creates pull requests to update outdated dependencies.
-
Enable Code Scanning Alerts under Security.
-
Include scanning actions in workflows:
- name: CodeQL Analysis uses: github/codeql-action/analyze@v2
- GitHub scans public repositories for leaked secrets and alerts the repository owner.
- Go to Settings → Account Security → Enable Two-Factor Authentication.
brew install gh # macOS
sudo apt install gh # Linuxgh auth login-
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
Authenticate using a personal access token:
curl -H "Authorization: token YOUR_TOKEN" https://api.github.com/user/reposcurl -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-
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.
