Git Cheatsheet
Quick reference for everyday Git commands and workflows.
Setup
# Identity
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Default editor and branch
git config --global core.editor vim
git config --global init.defaultBranch main
# Aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --decorate"
# View config
git config --list
git config --global --list
Basics
# Initialize / Clone
git init
git clone <url>
git clone <url> my-folder
git clone --depth 1 <url> # Shallow clone
# Status & Diff
git status
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Between branches
# Stage & Commit
git add <file>
git add .
git add -p # Interactive patch
git commit -m "message"
git commit --amend # Edit last commit (local only!)
Branches
# Create / Switch
git branch feature/login
git checkout feature/login
git checkout -b feature/login # Create and switch
git switch -c feature/login # Modern syntax
# List
git branch # Local
git branch -r # Remote
git branch -a # All
# Rename / Delete
git branch -m old-name new-name
git branch -d feature/login # Safe delete
git branch -D feature/login # Force delete
# Track remote branch
git checkout --track origin/feature
Remote
# Remote management
git remote -v
git remote add origin <url>
git remote rename origin upstream
git remote remove origin
git remote set-url origin <new-url>
# Fetch / Pull / Push
git fetch origin
git fetch --all
git pull # fetch + merge
git pull --rebase # fetch + rebase
git push origin main
git push -u origin feature # Set upstream
git push --force-with-lease # Safer than --force
# Delete remote branch
git push origin --delete feature/old
Merge & Rebase
# Merge
git merge feature/login
git merge --no-ff feature/login # Always create merge commit
git merge --squash feature # Squash into one commit
git merge --abort # Cancel in-progress merge
# Rebase
git rebase main # Rebase current onto main
git rebase -i HEAD~3 # Interactive rebase (last 3)
git rebase --abort
git rebase --continue # After resolving conflicts
# Cherry-pick
git cherry-pick <commit-hash>
git cherry-pick abc123..def456 # Range
History & Log
# Log
git log
git log --oneline --graph --all
git log -n 10 # Last 10 commits
git log --author="Name"
git log --since="2024-01-01"
git log --grep="fix:" # Search commit messages
git log -S "functionName" # Search code changes
git log -- path/to/file # History of file
# Show
git show <commit>
git show <commit>:path/to/file # File at commit
# Blame
git blame path/to/file
git blame -L 10,20 file # Lines 10-20
Undo
# Unstage
git restore --staged <file>
git reset HEAD <file> # Same effect
# Discard changes (working dir)
git restore <file>
git checkout -- <file> # Old syntax
# Revert a commit (safe — creates new commit)
git revert <commit>
git revert HEAD
# Reset (moves HEAD — rewrites history!)
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged
git reset --hard HEAD~1 # Discard changes completely
# Recover deleted file
git checkout HEAD -- deleted-file.txt
Stash
# Stash changes
git stash
git stash push -m "WIP: login form"
git stash push -u # Include untracked files
# List stashes
git stash list
# Apply stash
git stash pop # Apply + remove
git stash apply stash@{0} # Apply, keep stash
git stash drop stash@{0} # Remove stash
# Stash specific files
git stash push -m "partial" src/auth.py
# Create branch from stash
git stash branch feature/new stash@{0}
Tags
# Create tags
git tag v1.0.0 # Lightweight
git tag -a v1.0.0 -m "Release 1.0.0" # Annotated
git tag -a v1.0.0 <commit> # Tag past commit
# List / Show
git tag
git tag -l "v1.*"
git show v1.0.0
# Push tags
git push origin v1.0.0 # Single tag
git push origin --tags # All tags
# Delete tags
git tag -d v1.0.0 # Local
git push origin --delete v1.0.0 # Remote
Search
# Search in files (current state)
git grep "searchterm"
git grep -n "pattern" # With line numbers
git grep -l "pattern" # Files only
git grep "pattern" -- "*.py" # Specific files
# Find commit that introduced a bug (bisect)
git bisect start
git bisect bad # Current commit is bad
git bisect good v1.0.0 # v1.0.0 was good
# Test, then mark:
git bisect good # or: git bisect bad
git bisect reset # Done
# Find lost commits
git reflog
git fsck --lost-found
Git Workflows
Feature Branch Workflow
# 1. Always start from updated main
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/user-authentication
# 3. Work and commit often
git add -p
git commit -m "feat: add JWT token validation"
git commit -m "feat: add refresh token endpoint"
# 4. Keep branch up to date with main
git fetch origin
git rebase origin/main
# 5. Push and create PR
git push -u origin feature/user-authentication
# 6. After PR merged — clean up
git checkout main
git pull origin main
git branch -d feature/user-authentication
git push origin --delete feature/user-authentication
Conventional Commits
# Format: <type>[optional scope]: <description>
feat: add user login endpoint
fix: resolve token expiration bug
docs: update API documentation
style: format code with black
refactor: extract auth middleware
test: add unit tests for login service
chore: update dependencies
ci: add GitHub Actions workflow
perf: optimize database query with index
revert: revert "feat: add experimental feature"
# With scope
feat(auth): add OAuth2 Google login
fix(api): handle empty request body
test(auth): add integration tests for JWT
# Breaking change
feat!: change API response format
feat(api)!: remove deprecated /v1 endpoints
Resolving Merge Conflicts
# When a conflict occurs during merge/rebase:
# 1. See conflicted files
git status
# 2. Edit files — look for conflict markers:
# <<<<<<< HEAD
# Your changes
# =======
# Incoming changes
# >>>>>>> feature/branch
# 3. After resolving — mark as resolved
git add <resolved-file>
# 4. Continue
git merge --continue # or:
git rebase --continue
# Use a merge tool
git mergetool # Opens configured tool
git mergetool --tool=vimdiff
.gitignore Patterns
# .gitignore
# OS files
.DS_Store
Thumbs.db
# IDE
.idea/
.vscode/
*.swp
# Secrets — never commit!
.env
.env.local
*.pem
*.key
secrets.yml
# Build artifacts
dist/
build/
*.egg-info/
__pycache__/
*.pyc
*.class
# Dependencies
node_modules/
vendor/
.venv/
# Logs
*.log
logs/
# Test coverage
.coverage
htmlcov/
.pytest_cache/
# Terraform
*.tfstate
*.tfstate.backup
.terraform/
Useful Git Aliases
[alias]
# Short status
st = status -sb
# Pretty log
lg = log --oneline --graph --decorate --all
ll = log --pretty=format:"%C(yellow)%h%Creset %C(bold blue)%an%Creset %C(green)%ar%Creset %s"
# Undo last commit (keep changes)
undo = reset --soft HEAD~1
# Show aliases
aliases = config --get-regexp alias
# Stash shortcuts
save = stash push
pop = stash pop
# List contributors
contributors = shortlog --summary --numbered --no-merges
# Show files changed in last commit
changed = diff HEAD~1 HEAD --name-only
# Find branches containing commit
find-branch = branch -a --contains
💡 Tip: Add
git config --global pull.rebase true to always rebase instead of merge when pulling. Keeps history linear and clean.
⚠️ Golden Rules:
- Never
--forcepush tomainor shared branches — use--force-with-leaseat minimum - Never commit
.envfiles or secrets — rotate them if you do - Always
git pull --rebasebefore pushing to avoid unnecessary merge commits