← Cheat Sheets

Git Commands Cheat Sheet

The commands that cover 95% of real Git usage, grouped by what you are trying to do.

Everyday workflow

CommandWhat it does
git statusShow changed/staged files and branch state
git add | .Stage a file (or everything) for the next commit
git commit -m "msg"Commit staged changes
git pull --rebaseFetch remote and replay your commits on top
git push origin Publish local commits to the remote branch
git log --oneline -10Compact history of the last 10 commits
git diff --stagedShow staged changes not yet committed

Undoing things

CommandWhat it does
git restore Discard uncommitted changes in a file
git restore --staged Unstage a file, keep the changes
git commit --amendAdd staged changes to the last commit (local only!)
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard HEAD~1Undo last commit and delete its changes (dangerous)
git revert Create a new commit that undoes an old one (safe on shared branches)
git clean -fdDelete untracked files and directories (careful)

Branches & stash

CommandWhat it does
git switch -c Create and switch to a new branch
git switch Switch to an existing branch
git merge Merge a branch into the current one
git rebase Replay current branch commits on top of another branch
git branch -d Delete a merged branch
git stash / git stash popShelve changes / bring them back
git remote -vList configured remotes

Step-by-step recipes

GuideCovers
Undo the last commitsoft / mixed / hard reset compared, reflog rescue
Discard local changesrestore, clean, dry-runs, what is recoverable
Delete a branchlocal + remote, -d vs -D, pruning stale refs
Stash changespark work, untracked files, stacked stashes
Amend the last commitfix messages, fold in files, force-with-lease
Undo a pushed commitrevert vs reset, reverting merges

FAQ

git reset vs git revert — which one?
reset moves the branch pointer and (optionally) deletes commits — fine for local, unpushed work. revert adds an inverse commit — the only safe way to undo something already pushed to a shared branch.

git switch vs git checkout
checkout does three jobs (switch branches, create branches, restore files) and is easy to misuse. switch and restore split those jobs up; checkout still works everywhere.

I committed to the wrong branch — now what?
If unpushed: git reset --soft HEAD~1, switch to the right branch, commit again. If pushed: create the fix on the right branch and revert on the wrong one.

Try it instead of memorizing it

Generate the exact command in the Git Command Builder