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.

Questions

What is the difference between fetch and pull?

fetch downloads the remote's changes without touching your working tree; pull does that and immediately merges. Fetching first lets you look at what arrived before deciding how to integrate it.

How do I undo the last commit?

git reset --soft HEAD~1 keeps every change staged. The same command with --hard discards the work as well, which is the one-word difference worth being certain about before pressing enter.

What does reflog actually save?

Everywhere HEAD has been for the last few weeks, including commits no branch points at any more. It is why an accidental reset or a deleted branch is usually recoverable rather than gone.

Merge or rebase?

Merge preserves what actually happened; rebase produces a linear history that is easier to read. The rule that matters is not rebasing anything already pushed and shared, since it rewrites commits others hold.

Why does git say my branch has diverged?

Both you and the remote have commits the other lacks, usually after a rebase or an amend. Fetch and look at what is on each side before choosing between merging, rebasing, or force-pushing with lease.

Related in Cheat sheets