Git Commands Cheat Sheet
The commands that cover 95% of real Git usage, grouped by what you are trying to do.
Everyday workflow
| Command | What it does |
|---|---|
| git status | Show 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 --rebase | Fetch remote and replay your commits on top |
| git push origin | Publish local commits to the remote branch |
| git log --oneline -10 | Compact history of the last 10 commits |
| git diff --staged | Show staged changes not yet committed |
Undoing things
| Command | What it does |
|---|---|
| git restore | Discard uncommitted changes in a file |
| git restore --staged | Unstage a file, keep the changes |
| git commit --amend | Add staged changes to the last commit (local only!) |
| git reset --soft HEAD~1 | Undo last commit, keep changes staged |
| git reset --hard HEAD~1 | Undo last commit and delete its changes (dangerous) |
| git revert | Create a new commit that undoes an old one (safe on shared branches) |
| git clean -fd | Delete untracked files and directories (careful) |
Branches & stash
| Command | What 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 pop | Shelve changes / bring them back |
| git remote -v | List configured remotes |
Step-by-step recipes
| Guide | Covers |
|---|---|
| Undo the last commit | soft / mixed / hard reset compared, reflog rescue |
| Discard local changes | restore, clean, dry-runs, what is recoverable |
| Delete a branch | local + remote, -d vs -D, pruning stale refs |
| Stash changes | park work, untracked files, stacked stashes |
| Amend the last commit | fix messages, fold in files, force-with-lease |
| Undo a pushed commit | revert 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