Quick Reference · distributed version control

git cheat sheet

Every command moves a snapshot between four places: your working directory, the staging area, your local repo, and a remote. Learn the map once and the commands stop being a list to memorize.

working dir staging area local repo remote destructive most common

Distilled & cross-checked across: github.com/education · git-scm.com · atlassian.com/git · about.gitlab.com · cheat-sheets.org · rcs.bu.edu

The four areas & the commands that move work between them
Working Dir you edit files here git status git diff Staging Area the "index" — what's next git diff --staged Local Repo committed history (.git) git log Remote origin (e.g. GitHub) git remote -v git add git commit git push git reset git pull (= git fetch + git merge) YOUR MACHINE THE CLOUD
01Setup & Configonce per machine
02Start a Projectinit or clone
03Stage & Snapshotthe daily loop
04Branch & Mergeparallel work
05Share & Synctalk to the remote
06Inspect & Compareread history
07Stasha temporary shelf
08Taggingmark releases
09Undo & Rewritehandle with care
10Move & Removetrack path changes
11Ignoring Files.gitignore
Refer to a Commitanywhere you see <commit>

Combining diverged branches

Same goal — bring two branches back together — three different shapes of history. Based on the git-scm.com reference diagrams.

git merge

Creates a merge commit (M) with two parents. History keeps the true branched shape.

A B C M D E main feature

git rebase

Replays feature's commits on top of main as D′, E′. Linear history; the originals are rewritten.

A B C D′ E′ main

git merge --squash

Collapses all of feature's work into a single new commit on main. One tidy commit, no merge node.

A B C D+E main

fast-forward merge

main had no new commits, so its pointer just slides forward. No merge commit is created at all.

A B C D E main ▸ here now

Worth memorizing

fetch ≠ pullfetch downloads; pull = fetch + merge
reset ≠ revertreset rewrites history; revert adds an undo commit
-d vs -Dlowercase = safe; UPPERCASE = force
add . ≠ commit -acommit -a skips brand-new untracked files
.git/configper-repo config  ·  ~/.gitconfig = global
reflogrecovers "lost" commits after a bad rebase/reset
-u on first pushsets upstream so later push/pull need no args
force-with-leasealways prefer over plain --force