Cheat sheetGit
Git essentials
The Git commands you reach for weekly — grouped by what you're trying to do, with the undo table you'll want at 5 pm on a Friday.
Last updated
Everyday flow
| Command | What it does |
|---|---|
| git status | What's changed, what's staged, what branch you're on — the command between all other commands. |
| git add -p | Stage hunk by hunk, approving each — how clean, single-purpose commits get made. |
| git commit -m "msg" | Commit what's staged with a message. |
| git pull --rebase | Fetch and replay your local commits on top — linear history, no merge-bubble commits. |
| git push | Publish your commits to the remote branch. |
| git log --oneline -10 | The last ten commits, one line each. |
Branches
| Command | What it does |
|---|---|
| git switch -c feature-x | Create a branch and move to it in one step. |
| git switch - | Jump back to the previous branch — the cd - of Git. |
| git branch -d name | Delete a merged branch (-D to force an unmerged one). |
| git merge feature-x | Bring that branch's commits into this one. |
| git rebase main | Replay this branch onto main's tip — never on commits others already pulled. |
Undo (the Friday table)
| Command | What it does |
|---|---|
| git restore file | Throw away unstaged edits to a file — gone for good, so be sure. |
| git restore --staged file | Un-stage a file; the edits stay in your working tree. |
| git commit --amend | Redo the last commit — message or contents — if you haven't pushed it. |
| git revert abc123 | Undo a pushed commit safely, by adding a new commit that reverses it. |
| git reset --hard HEAD~1 | Erase the last local commit AND its changes — only for commits nobody else has. |
| git reflog | Every position HEAD has held — how "lost" commits get found again. |
Stash & inspect
| Command | What it does |
|---|---|
| git stash | Shelve uncommitted changes to get a clean tree; git stash pop brings them back. |
| git stash list | What's on the shelf. |
| git diff | Unstaged changes (--staged for what's about to be committed). |
| git blame -L 10,20 file | Who last touched lines 10–20, and in which commit. |
| git bisect start | Binary-search history for the commit that broke things — mark good/bad, Git halves the rest. |
Worth remembering
- →The distinction that keeps history safe: revert for anything pushed, reset only for commits still private to your machine.
- →If a command's output ever surprises you, git reflog is the safety net — commits are rarely lost, only unreferenced.