I've been using Git for over a decade, and I still look up commands. Not because I'm forgetful -- because Git has a massive surface area and you only use 20% of it daily. The other 80% comes up when you're untangling a messy rebase at midnight or trying to find which commit introduced a bug. This is the cheat sheet I keep open in a terminal tab at all times.
1. Setup & Configuration
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --decorate"
git config --list
Config Levels
-
System (
--system) --/etc/gitconfig -
Global/user (
--global) --~/.gitconfig -
Local/repo (
--local) --.git/config
2. Creating & Cloning Repos
git init # Init repo in current directory
git clone <url> # Clone repo
git clone --depth 1 <url> # Shallow clone (latest commit only)
git clone -b dev <url> # Clone specific branch
3. The Basics -- Snapshot Workflow
git status # Show working tree status
git add <file> # Stage a file
git add . # Stage all changes
git add -p # Stage changes interactively (hunks)
git commit -m "message" # Commit staged changes
git commit --amend # Modify last commit
git diff # Unstaged changes vs last commit
git diff --staged # Staged changes vs last commit
git diff branch1..branch2 # Diff between two branches
4. Viewing History
git log --oneline --graph # Branch graph view
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 -S "function_name" # Search changes in code (pickaxe)
git show <commit> # Show commit details
git blame <file> # Show who changed each line
git blame -L 10,20 <file> # Blame specific lines
5. Branching
git branch # List local branches
git branch -a # List all branches
git switch -c dev # Create + switch (modern syntax)
git switch dev # Switch to branch
git branch -m old-name new-name # Rename branch
git branch -d dev # Delete branch (safe)
git branch -D dev # Force delete branch
git branch --merged # Branches merged into current
Branch Naming Conventions
-
feature/user-auth-- New features -
fix/login-crash-- Bug fixes -
hotfix/null-pointer-- Urgent prod fix -
chore/update-deps-- Maintenance -
release/v2.1.0-- Release prep
6. Merging
git merge dev # Merge dev into current branch
git merge --no-ff dev # Always create merge commit
git merge --squash dev # Squash all commits into one
git merge --abort # Abort an in-progress merge
7. Rebasing
git rebase main # Rebase current branch onto main
git rebase --abort # Abort rebase
git rebase --continue # Continue after resolving conflict
git rebase -i HEAD~3 # Interactive rebase: rewrite last 3 commits
Interactive Rebase Commands
-
pick(p) -- Keep commit as-is -
reword(r) -- Keep commit, edit message -
squash(s) -- Combine with previous commit -
fixup(f) -- Like squash, discard message -
drop(d) -- Delete the commit
8. Remote Operations
git remote -v # List remotes with URLs
git fetch --prune # Fetch + remove stale remote branches
git pull --rebase # Fetch + rebase
git push -u origin main # Push + set upstream tracking
git push --force-with-lease # Safe force push
git push origin --delete dev # Delete remote branch
Prefer --force-with-lease over --force -- it fails if someone else pushed since your last fetch.
9. Stashing
git stash # Stash current changes
git stash push -m "WIP: login" # Stash with a name
git stash -u # Include untracked files
git stash list # List all stashes
git stash pop # Apply latest stash + drop it
git stash apply stash@{2} # Apply specific stash (keep it)
git stash drop stash@{0} # Delete specific stash
git stash branch feature stash@{0} # Create branch from stash
10. Undoing Things
# Undo Working Directory Changes
git restore <file> # Discard changes (modern)
git clean -fd # Remove untracked files + dirs
# Undo Staging
git restore --staged <file> # Unstage (modern syntax)
# Undo Commits
git revert <commit> # New commit that undoes a commit (safe)
git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset --mixed HEAD~1 # Undo commit, keep changes unstaged
git reset --hard HEAD~1 # Undo commit, DISCARD changes
Reset Modes Comparison
- --soft -- HEAD moved, staging unchanged, working dir unchanged
- --mixed -- HEAD moved, staging cleared, working dir unchanged
- --hard -- HEAD moved, staging cleared, working dir cleared (dangerous)
11. Cherry-Picking & Tags
git cherry-pick <commit> # Apply a specific commit
git tag -a v1.0.0 -m "Release" # Create annotated tag
git push origin --tags # Push all tags
12. Working with History -- Advanced
# Find which commit introduced a bug
git bisect start
git bisect bad # Current commit is broken
git bisect good v1.2.0 # This version was fine
git bisect reset # End bisect session
# Find a lost commit
git reflog # Log of ALL HEAD movements
git branch recovered <lost-hash> # Save it as a branch
Common Workflows
Feature Branch Workflow
git switch main && git pull
git switch -c feature/my-feature
# ... do work ...
git add -p && git commit -m "feat: add X"
git push -u origin feature/my-feature
# Open Pull Request -> merge -> delete branch
Hotfix Workflow
git switch main && git pull
git switch -c hotfix/critical-bug
git commit -am "fix: resolve null pointer"
git switch main
git merge --no-ff hotfix/critical-bug
git tag -a v1.0.1 -m "Hotfix release"
git push && git push --tags
Pro Tips
-
git reset --hardis permanent -- changes in working directory are gone. Usegit stashto be safe. - Never rebase shared/public branches -- it rewrites history and causes chaos for teammates.
-
git add -pis your best friend -- review exactly what you're committing, hunk by hunk. -
git reflogis your safety net -- almost anything can be recovered within 90 days.
If you found this useful, share it with a colleague who needs it. Subscribe for more developer resources every week.
Want the full resource?
Git Cheat Sheet — $6.99 on Gumroad
Get the complete, downloadable version. Perfect for bookmarking, printing, or sharing with your team.
If you found this useful, drop a ❤️ and follow for more developer resources every week.
Top comments (0)