// REF. 05 / VERSION CONTROL
Git cheat sheet
Git has two kinds of users: the ones who know reflog exists and the ones who are about to. This mat is for both. Every key is a real command (drop the git prefix, it is implied) checked against the official manual pages as of Git 2.56, which means switch and restore instead of the checkout-does-everything habit.
Two habits worth building off this sheet: stage with add -p so every commit says one thing, and push with --force-with-lease so the one time you rewrite history you do not also rewrite a colleague’s. Everything else is in the UNDO column, which is where most of us live.
↓ Download the printable PDF free · letter size · no email needed
Start
init -b main- new repo, first branch named main
clone --depth=1 <url>- shallow clone, latest commit only
clone --filter=blob:none- partial clone; file contents on demand
status -sb- short status, with the branch line
config set --global k v- write a setting to ~/.gitconfig
Stage & commit
add -p- stage hunk by hunk
diff --staged- what the next commit will contain
restore --staged <f>- unstage, keep the edits
restore <f>- discard unstaged edits — (no undo)
commit -am "msg"- stage tracked changes and commit
commit --amend- redo last commit; --no-edit keeps message
Branches
switch -c <name>- create a branch and switch to it
switch -- previous branch
branch -vv- branches, upstreams, ahead/behind
branch -d <name>- delete a merged branch; -D forces
branch -m <new>- rename the current branch
switch --detach <sha>- look at a commit without a branch
Sync
fetch --prune- update remote refs, drop deleted ones
pull --rebase- fetch, replay your commits on top
push -u origin <br>- push and remember the upstream
push --force-with-lease- force, unless the remote moved on
push -d origin <br>- delete the remote branch
remote -v- remotes and their urls
History
log --oneline --graph- compact graph; --all for every branch
log -p -- <path>- commits touching a path, with diffs
log -S"text"- commits that added or removed text
log A..B- commits in B that are not in A
log --format="%h %s"- custom lines; %an author, %ar age, %d refs
show <sha>:<path>- a file as it was in that commit
blame -L 10,20 -w <f>- who changed lines 10-20, ignoring space
Rewrite
rebase -i HEAD~3- reorder, squash or edit the last 3
pick reword edit- keep / change message / stop to amend
squash fixup drop- fold in / fold in silently / delete
commit --fixup=<sha>- queue a fix; rebase --autosquash folds it
rebase --onto A B- replay the commits after B onto A
rebase --continue- resume after resolving; --abort bails out
cherry-pick <sha>- copy one commit onto this branch
Stash & worktree
stash -u- shelve changes, untracked files too
stash pop- reapply the latest and drop it
stash list- stash@{0} is the newest
stash show -p- full diff of the latest stash
worktree add -b x ../x- branch x checked out in another dir
worktree list- every checkout of this repo
Undo
reflog- everywhere HEAD has been; your safety net
reset --hard HEAD@{1}- back to where HEAD was one move ago
reset --soft HEAD~1- uncommit, keep the changes staged
revert <sha>- new commit that undoes an old one
bisect start HEAD v1.2- bad first, good second; then answer it
bisect run <cmd>- let a script answer good or bad
merge --abort- walk away from a conflicted merge
Here be dragons
push --force- overwrites the remote, and your teammates
reset --hard- uncommitted edits are gone; not in reflog
clean -fdx- also deletes ignored files, like your .env
checkout- three jobs, one verb; switch and restore
// KEEP IT UNDER YOUR KEYBOARD
Git Field Manual Desk Mat
from $34 · printed to order in the USA