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

CommandWhat it does
git statusWhat's changed, what's staged, what branch you're on — the command between all other commands.
git add -pStage 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 --rebaseFetch and replay your local commits on top — linear history, no merge-bubble commits.
git pushPublish your commits to the remote branch.
git log --oneline -10The last ten commits, one line each.

Branches

CommandWhat it does
git switch -c feature-xCreate a branch and move to it in one step.
git switch -Jump back to the previous branch — the cd - of Git.
git branch -d nameDelete a merged branch (-D to force an unmerged one).
git merge feature-xBring that branch's commits into this one.
git rebase mainReplay this branch onto main's tip — never on commits others already pulled.

Undo (the Friday table)

CommandWhat it does
git restore fileThrow away unstaged edits to a file — gone for good, so be sure.
git restore --staged fileUn-stage a file; the edits stay in your working tree.
git commit --amendRedo the last commit — message or contents — if you haven't pushed it.
git revert abc123Undo a pushed commit safely, by adding a new commit that reverses it.
git reset --hard HEAD~1Erase the last local commit AND its changes — only for commits nobody else has.
git reflogEvery position HEAD has held — how "lost" commits get found again.

Stash & inspect

CommandWhat it does
git stashShelve uncommitted changes to get a clean tree; git stash pop brings them back.
git stash listWhat's on the shelf.
git diffUnstaged changes (--staged for what's about to be committed).
git blame -L 10,20 fileWho last touched lines 10–20, and in which commit.
git bisect startBinary-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.

Related in Cheat sheets