Git is the standard version control system for developers, but with over 150 commands and dozens of flags, finding the right one at the right time is the challenge. This Git commands cheat sheet organizes 80+ essential Git commands by workflow — setup, daily commit, branching, history, remote, and fixing mistakes — so the right command is found in seconds, not searched for the tenth time.
Unlike alphabetical lists, this guide explains what each command does, when to use it, and the exact copy-paste form, from git status to rebase, stash, cherry-pick, and reflog, based on the official Git documentation.
git status → git add → git commit → git push and git pull --rebase to update. For a full workflow with branching, fixes, and history, use the Git command cheat sheet below — organized by task with copy-paste examples for setup, commit, branch, merge, remote, and undo.
Git Mental Model — The 4 Areas
Confusion disappears once the four areas are clear. Every file lives in one of these, and Git commands move files between them.
- Working Directory: Your files as edited.
git statusshowsmodifiedoruntrackedhere. - Staging Area (Index): Proposed next commit.
git addmoves changes here;git diff --stagedshows what's staged. - Local Repository: Commit history in
.git/objects.git commitmoves staged changes here;git logshows it. - Remote: Shared history on GitHub/GitLab (
origin/main).git pushuploads,git pullorgit fetchdownloads.
Key moves: add = Working → Staging, commit = Staging → Repo, push = Repo → Remote. Nearly every workflow is a combination of these.
Everyday Flow — 8 Commands That Cover 80%
git status— what's changed?git add .— stage all (orgit add -pfor interactive patch)git commit -m "feat: add login"— save locallygit push— share to remotegit pull --rebase origin main— update from main without merge commitgit log --oneline --graph --all— readable historygit branch -a— see branchesgit checkout -b feature/login— new branch (orgit switch -c)
# Feature branch example
git checkout -b feature/login
git status # see modified: app.js
git add app.js
git commit -m "feat: add login"
git push -u origin feature/login
git pull --rebase origin main # update before PR
Essential Git Commands — By Category (30 Commands)
1. Setup and Configuration (Once Per Machine or Repo)
git init # new repo in current dir
git clone https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/repo.git # shallow
git config --global user.name "Anna Lee"
git config --global user.email "anna@example.com"
git config --global init.defaultBranch main
git remote -v # show remotes
git remote add origin https://github.com/user/repo.git
2. Staging and Commit (Daily Loop)
git status # what's changed
git add <file> # stage one file
git add . # stage all
git add -p # stage interactively (hunk by hunk)
git diff # unstaged changes
git diff --staged # staged changes
git commit -m "feat: add login" # commit with message
git commit --amend # fix last commit (message or add files)
Use Conventional Commits (feat:, fix:, docs:) for readable history and automated changelogs.
3. Branching (One Branch Per Feature)
git branch # list local branches
git branch -a # all incl. remote
git branch -d feature/x # delete merged branch
git checkout -b feature/x # create + switch (classic)
git switch -c feature/x # create + switch (newer)
git merge feature/x # merge into current branch
git rebase main # replay current branch on main
4. History and Log
git log --oneline --graph --all # compact graph
git log --stat # files changed per commit
git log -p # diff per commit
git show a1b2c3d # single commit
git blame app.js # who changed each line
git reflog # history of HEAD moves (recovery)
reflog is the safety net — it shows every HEAD move even after reset --hard, allowing recovery.
5. Remote (Share and Update)
git fetch # download without merge
git pull # fetch + merge
git pull --rebase # fetch + rebase (cleaner history)
git push # upload
git push -u origin feature/x # push + set upstream
git push --force-with-lease # safer force push (fails if remote moved)
Prefer --force-with-lease over --force — it aborts if someone else pushed in the meantime. Never force-push shared main.
6. Undo and Fix
git restore --staged <file> # unstage (newer)
git restore <file> # discard working changes
git reset --soft HEAD~1 # undo last commit, keep staged
git reset --hard HEAD~1 # undo last commit + discard changes (dangerous)
git revert a1b2c3d # new commit that undoes a commit (safe for shared)
git stash push -m "wip" # save uncommitted for later
git stash pop # apply + drop last stash
| Situation | Safe Command |
|---|---|
| Staged wrong file | git restore --staged <file> |
| Committed too early (private branch) | git reset --soft HEAD~1 |
| Need to undo a commit on shared main | git revert <hash> |
| Not ready to commit but must switch branch | git stash push -m "wip" then git stash pop |
Advanced Git — Rebase, Stash, Cherry-Pick and Reflog
Rebase — Clean, Linear History
git rebase main replays the current branch's commits on top of main, avoiding merge commits. git rebase -i HEAD~3 opens an interactive editor to pick, squash, reword, or drop the last 3 commits — ideal for squashing "wip" commits before a PR. git pull --rebase does fetch + rebase in one. Rule: never rebase shared main — rewrite only private branches.
Stash — Save for Later
Stash saves uncommitted changes without a commit. git stash push -m "wip" saves, git stash list shows the stack, and git stash pop applies and drops the last stash. Use when needing to switch branches but not ready to commit.
Cherry-Pick — Copy One Commit
git cherry-pick a1b2c3d copies a single commit to the current branch — surgical, unlike merge which brings a whole branch. Useful for hotfixes that need to land on both main and a release branch.
Reset vs Revert vs Restore
reset --soft— moves HEAD, keeps staging (undo commit, keep changes staged)reset --hard— moves HEAD and discards working and staging (dangerous, local only)revert— creates a new commit that undoes the target (safe for shared history)restore— unstages or discards a file without touching history
Reflog — The Safety Net
git reflog shows every move of HEAD, including after reset --hard or a bad rebase. A commit that seems lost can be recovered with git reset --hard HEAD@{2}. Check git-reflog for retention details.
Common Recipes — Copy-Paste
# Undo last commit but keep changes staged
git reset --soft HEAD~1
# → edit → git commit -m "fix: correct message"
# Squash last 3 commits into one
git rebase -i HEAD~3 # change to: pick, squash, squash → save
# Sync fork with upstream
git fetch upstream
git rebase upstream/main
git push
# Stash, switch, and restore
git stash push -m "wip"
git checkout main
git stash pop
Git Cheat Sheet Quick Reference — Printable One Page
| Task | Command |
|---|---|
| Start new repo | git init && git add . && git commit -m "init" |
| Clone | git clone https://github.com/user/repo.git |
| New feature | git checkout -b feature/x && git push -u origin feature/x |
| Save work | git add -A && git commit -m "feat: add x" |
| Update from main | git pull --rebase origin main |
| Share | git push or git push --force-with-lease |
| See history | git log --oneline --graph --decorate --all |
| Undo | git restore, git reset --soft, git revert, git stash |
3 Safety Rules That Prevent 90% of Disasters
- Don't
--forceshared main — use--force-with-leaseso the push fails if the remote moved - Don't rebase shared history — rebase only private feature branches
- Commit or stash before switching — uncommitted changes can cause conflicts or be overwritten
How to Use Git Effectively — Beyond Commands
- Commit messages matter: Use imperative mood ("add login" not "added") and Conventional Commits for automation. A good message explains why, not just what.
- Branch per feature/fix: Keeps history reviewable and enables parallel work. Delete merged branches (
git branch -d) to stay tidy. - Pull with rebase for linearity:
git pull --rebaseavoids noisy merge commits on shared branches. - Review before push:
git diff --stagedandgit log --statcatch mistakes locally.
FAQs About Git Commands
What is the difference between git fetch and git pull?
git fetch downloads remote changes without merging; git pull is fetch + merge (or fetch + rebase with --rebase). Fetch is safe to inspect before integrating.
How do I undo the last commit?
On a private branch, git reset --soft HEAD~1 undoes the commit but keeps changes staged for re-committing. On shared main, use git revert HEAD which creates a new commit that undoes the change without rewriting history.
What does git rebase do?
Rebase replays commits on top of another branch for a linear history. git rebase main moves the current branch's commits onto main; git rebase -i HEAD~3 lets the last 3 commits be squashed or reworded. Avoid rebasing shared branches.
How do I save changes without committing?
Use git stash push -m "wip" to save uncommitted changes, switch branches, then git stash pop to restore. git stash list shows saved stashes.
What is the difference between merge and rebase?
Merge creates a merge commit preserving branch history; rebase replays commits linearly without a merge commit. Merge is safe for shared branches; rebase keeps private feature history clean.
How do I recover a lost commit after reset?
Use git reflog to find the commit hash or HEAD@{n} reference, then git reset --hard <hash> or git cherry-pick <hash>. Reflog retains HEAD moves for ~90 days by default.
Conclusion
Mastering Git is not about memorizing every flag — it's about knowing the workflow: Working → Staging → Repo → Remote, and which command moves files between them. Start with the daily 8 (status, add, commit, push, pull --rebase, log, branch, checkout), then layer branching and fixing tools as needed.
Keep this cheat sheet bookmarked for quick lookup — organized by task, with copy-paste examples for every stage from setup to recovery.