Overview

This card collects the git commands worth memorizing, grouped by intent. The full reference is git help <command>; this page is the one to skim when you need the right invocation in under ten seconds. For the principles behind these commands (rebase vs merge, conventional commits, fixups), see git.

Inspect

Read state before you change it. These are the read-only commands.

CommandWhat it does
git status -sbShort branch summary; one line per file.
git log --oneline -20Last 20 commits, terse.
git log --oneline --graph --allBranch topology across all refs.
git log -p <file>Full diff history for one file.
git log -S "needle"Commits that added or removed the literal “needle”.
git log --grep "pattern"Commits whose message matches the pattern.
git blame -L 10,20 <file>Annotate lines 10 to 20 with the last commit that touched each.
git diffUnstaged changes.
git diff --stagedChanges already in the index.
git diff main...HEADWhat this branch adds on top of main.
git show <ref>Show a commit, tag, or tree object.
git reflogLocal history of HEAD moves; the recovery log.

Reach for git log --oneline --graph --all when a teammate says “my branch is gone.” It rarely is.

Undo

Choose the undo that matches the scope. The wrong one rewrites public history.

CommandWhat it undoes
git restore <file>Discard unstaged changes in working tree.
git restore --staged <file>Unstage; leave working tree alone.
git restore --source=HEAD~1 <file>Roll one file back to the previous commit.
git commit --amendReplace the tip commit. Local only.
git reset --soft HEAD~1Undo the last commit; keep changes staged.
git reset --mixed HEAD~1Undo the last commit; keep changes unstaged.
git reset --hard HEAD~1Undo the last commit; throw the work away.
git revert <ref>Create a new commit that inverts an old one. Safe on shared branches.
git clean -fdDelete untracked files and directories. No undo.

Never --amend or reset a commit that has been pushed to a shared branch. Use revert there.

Branch

Branches are pointers. Treat them like cheap bookmarks.

CommandEffect
git switch <branch>Move HEAD; modern replacement for checkout.
git switch -c <branch>Create and switch in one step.
git switch -c <branch> origin/<branch>Track a remote branch.
git branch -d <branch>Delete a merged branch.
git branch -D <branch>Delete a branch regardless of merge state.
git branch --merged mainList branches fully merged into main.
git branch -m old newRename a branch.
git worktree add ../wt branchCheck out a second working copy.

Prefer switch and restore over the older checkout. They do not overload one verb for two operations.

Sync

Fetch, then decide. pull is fetch plus a merge or rebase; the merge surprises people.

CommandEffect
git fetch --all --pruneUpdate remote-tracking refs; drop deleted ones.
git pull --ff-onlyRefuse to merge; abort if a fast-forward is impossible.
git pull --rebaseReplay local commits on top of the remote tip.
git push -u origin <branch>Push and set upstream.
git push --force-with-leaseForce-push but refuse if the remote moved.

Configure pull.ff = only globally; require explicit intent when a merge is wanted.

Rebase, stash, recover

Rewrite history locally; rescue work from the reflog.

CommandEffect
git rebase mainReplay this branch on top of main.
git rebase -i HEAD~5Edit the last 5 commits (squash, reorder, drop).
git commit --fixup=<ref>Mark a commit as fixup for <ref>; autosquash later.
git rebase -i --autosquash mainApply fixups automatically.
git stash push -m "wip"Stash with a label.
git stash listList stashes.
git stash popApply the top stash and drop it.
git reflogShow every HEAD move; the lifeline for “lost” commits.
git reset --hard <reflog-ref>Resurrect a discarded commit.

The reflog keeps entries for 90 days by default. Almost nothing in git is permanently lost.

Common gotchas

  • git pull defaults to a merge; configure pull.ff = only or pull.rebase = true so it cannot create a surprise merge commit.
  • git reset --hard discards uncommitted work without confirmation. Run git stash first if you might want it back.
  • git push --force overwrites the remote even if a teammate pushed since you fetched. Use --force-with-lease.
  • git rebase of a branch already pushed to a shared remote forces collaborators to recover. Rebase locally; merge after pushing.
  • git clean -fd deletes untracked files including .env. Run git clean -nd first to see what it would remove.