โ Back to Cheat Sheets
๐ GitHub & Git Cheat Sheet
Complete Git & GitHub reference โ branching, merging, rebasing, cherry-pick, tags, hooks, GitHub Actions, and PR workflow.
Setup & Config
Init & Clone
git init # New repo
git clone https://github.com/user/repo.git
git clone --depth 1 https://... # Shallow clone
git clone -b branch-name https://... # Clone specific branch
git remote add origin https://...
git remote -v # List remotes
git remote set-url origin new-url # Change remote URLInitialize, clone, and configure remotes.
Git Config
git config --global user.name 'Your Name'
git config --global user.email 'you@email.com'
git config --global core.editor 'code --wait'
git config --global init.defaultBranch main
git config --global pull.rebase true # Rebase on pull
git config --list # Show all config
# Per-repo config
git config user.email 'work@company.com'Configure Git identity and preferences.
.gitignore
# .gitignore
node_modules/
*.pyc
__pycache__/
.env
.env.local
*.log
dist/
build/
.DS_Store
*.swp
.vscode/
.idea/
# Negate (include specific file)
!important.log
# Already tracked? Remove from tracking:
git rm --cached file.txt
git rm -r --cached folder/Ignore files and remove already-tracked files.
Daily Workflow
Status & Stage
git status # Check working tree
git status -s # Short format
git add file.txt # Stage specific file
git add . # Stage all changes
git add -p # Interactive hunk staging
git add -A # Stage all (including deletes)
git reset HEAD file.txt # Unstage fileCheck status and selectively stage changes.
Commit
git commit -m 'feat: add login page'
git commit -am 'quick fix' # Stage tracked + commit
git commit --amend # Edit last commit message
git commit --amend --no-edit # Amend without msg change
git commit --allow-empty -m 'trigger CI'
# Conventional commits
feat: # New feature
fix: # Bug fix
docs: # Documentation
style: # Formatting
refactor: # Code refactor
test: # Tests
chore: # MaintenanceCreate commits with conventional commit format.
Push & Pull
git push origin main
git push -u origin feature/login # Set upstream
git push --force-with-lease # Safe force push
git push origin --delete old-branch # Delete remote branch
git pull origin main # Fetch + merge
git pull --rebase origin main # Fetch + rebase
git fetch --all # Fetch without merge
git fetch --prune # Clean stale remote refsSync with remote โ push, pull, fetch.
Branching
Create & Switch
git branch # List local branches
git branch -a # List all (incl. remote)
git branch -r # List remote only
git checkout -b feature/api # Create & switch
git switch -c feature/api # Modern create & switch
git switch main # Switch to branch
git branch -d merged-branch # Delete (safe)
git branch -D unmerged-branch # Force delete
git branch -m old-name new-name # Rename
git branch --merged # Branches merged into currentFull branch lifecycle โ create, switch, list, delete, rename.
Merge
# Fast-forward merge (linear history)
git merge feature/api
# No fast-forward (always create merge commit)
git merge --no-ff feature/api
# Squash merge (combine all commits into one)
git merge --squash feature/api
git commit -m 'feat: api feature'
# Abort a conflicted merge
git merge --abortMerge strategies โ fast-forward, no-ff, squash.
Rebase
# Rebase current branch onto main
git rebase main
# Interactive rebase (squash, edit, reorder last 3)
git rebase -i HEAD~3
# In editor:
# pick abc1234 First commit
# squash def5678 Second commit (squash into first)
# reword ghi9012 Third commit (edit message)
# Abort rebase
git rebase --abort
# Continue after resolving conflicts
git rebase --continueRebase for linear history โ interactive squash, reorder.
Undo & Fix
Discard Changes
git checkout -- file.txt # Discard unstaged changes
git restore file.txt # Modern discard
git restore --staged file.txt # Unstage file
git clean -fd # Remove untracked files
git clean -fdn # Dry run firstDiscard working directory and staging changes.
Reset
git reset --soft HEAD~1 # Undo commit, keep staged
git reset --mixed HEAD~1 # Undo commit, unstage (default)
git reset --hard HEAD~1 # Undo commit + all changes
git reset --hard origin/main # Reset to remote state
# Reset specific file to last commit
git checkout HEAD -- file.txtReset HEAD โ soft (keep staged), mixed, hard (lose all).
Revert & Reflog
# Revert: create new commit that undoes changes (safe)
git revert <commit-sha>
git revert HEAD # Undo last commit
git revert --no-commit HEAD~3..HEAD # Revert range
# Reflog: recover "lost" commits
git reflog # History of HEAD movements
git checkout <reflog-sha> # Recover lost commit
git branch recovery <sha> # Create branch from lost commitSafe undo with revert, recover lost work with reflog.
History & Inspection
Log
git log --oneline -10 # Compact last 10
git log --graph --all --oneline # Visual branch graph
git log --author='Alice' # Filter by author
git log --since='2024-01-01' # Filter by date
git log --grep='fix' # Search commit messages
git log -p file.txt # Changes to specific file
git log --stat # Files changed per commit
git shortlog -sn # Commits per authorInspect commit history with filters.
Diff & Blame
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Between branches
git diff HEAD~3 # Last 3 commits
git diff --name-only main # Changed file names only
git blame file.py # Who changed each line
git blame -L 10,20 file.py # Blame specific lines
git show <commit-sha> # Show specific commit
git show HEAD:file.txt # Show file at commitCompare changes and trace line authorship.
Stash, Tags & Cherry-pick
Stash
git stash # Stash changes
git stash -m 'work in progress' # Named stash
git stash -u # Include untracked files
git stash list # List all stashes
git stash pop # Apply + remove latest
git stash apply stash@{1} # Apply specific stash
git stash drop stash@{0} # Remove specific
git stash clear # Remove all stashes
git stash show -p stash@{0} # View stash diffShelve and restore uncommitted changes.
Tags
git tag v1.0.0 # Lightweight tag
git tag -a v1.0.0 -m 'Release 1.0' # Annotated tag
git tag -a v1.0.0 <commit-sha> # Tag specific commit
git push origin v1.0.0 # Push single tag
git push origin --tags # Push all tags
git tag -d v1.0.0 # Delete local tag
git push origin --delete v1.0.0 # Delete remote tag
git tag -l 'v1.*' # List matching tagsCreate, push, and manage release tags.
Cherry-pick
git cherry-pick <commit-sha> # Apply specific commit
git cherry-pick abc123 def456 # Multiple commits
git cherry-pick --no-commit <sha> # Apply without committing
git cherry-pick --abort # Abort on conflictApply specific commits from another branch.
GitHub-Specific
Pull Request Workflow
# 1. Create feature branch
git checkout -b feature/new-api
# 2. Make changes, commit, push
git add . && git commit -m 'feat: new api'
git push -u origin feature/new-api
# 3. Create PR on GitHub (or CLI)
gh pr create --title 'New API' --body 'Description'
# 4. Review, approve, merge on GitHub
# 5. Clean up
git checkout main && git pull
git branch -d feature/new-apiStandard feature branch to PR workflow.
GitHub CLI (gh)
gh repo create my-repo --public
gh repo clone user/repo
gh pr list
gh pr create --fill # Auto-fill from commits
gh pr checkout 42 # Checkout PR #42
gh pr merge 42 --squash
gh issue create --title 'Bug' --body 'Details'
gh issue list --label 'bug'
gh release create v1.0 --generate-notesGitHub CLI for repos, PRs, issues, and releases.
GitHub Actions (Basics)
# .github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- run: pytest tests/Basic CI/CD pipeline with GitHub Actions.