Git cheatsheet
14 commands you use every week. main runs along the bottom, feature along the top, and ◀ marks where HEAD is. The sheet plays them one after another; pick one to watch it again.
git add -AMove your edits to the staging area, ready to commitSave work
Branches
Undo
Look
Save work
git add -AMove your edits to the staging area, ready to commitgit commit -m "C"What's staged becomes a new commit; the branch moves to itgit stashPut unfinished edits aside; your files go back to the last commitgit stash popBring the stashed edits back and drop them from the stashBranches
git switch -c featureMake a branch where you are and move HEAD to itgit switch mainMove HEAD to another branch; your files followgit merge featureJoin another branch in: a merge commit with two parents, all history keptgit rebase mainReplay this branch's commits on top of main: new commits (new ids), a straight linegit cherry-pick featureCopy one commit (feature's tip) onto this branchUndo
git revert HEADA new commit that undoes the last one; history stays. Safe for what's pushedgit reset --hard HEAD~1Move the branch back one commit and throw that work away. Never on what's pushedgit reset --soft HEAD~1Move the branch back one commit, keeping its changes staged to commit againLook
git statusWhich branch you're on, what's changed, what's staged. Changes nothinggit log --oneline --graph --allDraws every branch's commit graph in the terminal. Changes nothingThe three that people mix up
merge or rebase?
merge keeps both lines and joins them with a commit that has two parents. rebase copies your commits on top of the other branch as new ones and leaves the old ones behind, so the line is straight. Rebase only commits nobody else has pulled.
revert or reset?
revert adds a commit that undoes another: safe for anything already pushed. reset moves the branch back; --hard throws the work away, --soft keeps it staged. Reset only what nobody else has.
Lost a commit?
After a reset --hard or a rebase, the old commits aren't on any branch, but they aren't gone yet: git reflog lists where HEAD has been, and git switch -c rescue <id> brings one back.
Checked
#!/bin/sh
# Every command on the Git cheatsheet, run for real: each one in a fresh repository set up as its "before" picture,
# then one line saying what the repository looks like after (each branch's first-parent line, HEAD, the counts).
# The page's pictures are held to these lines (lib/cheat-git.test.ts). Run: sh git.sh (needs git)
set -eu
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
G() { git -c user.name=mindloop -c user.email=mindloop@example.invalid -c init.defaultBranch=main -c advice.detachedHead=false "$@"; }
commit() { echo "$1" >> notes.txt; G add -A; G commit -q -m "$1"; }
fresh() { rm -rf "$work/r"; mkdir "$work/r"; cd "$work/r"; G init -q; commit A; commit B; }
three() { fresh; commit C; }
diverged() { fresh; G switch -q -c feature; echo F1 > f1.txt; G add -A; G commit -q -m F1; G switch -q main; commit C; G switch -q "$1"; }
edit() { echo "half done" >> notes.txt; }
say() { # what the repository looks like now, in the words of lib/cheat-git.ts summary()
out=""
for b in main feature; do
if G rev-parse -q --verify "refs/heads/$b" >/dev/null; then
line=$(G log --first-parent --reverse --format='%s%x09%p' "$b" | awk -F'\t' '{ n = split($2, p, " "); s = $1; sub(/^Merge branch .*/, "M", s); sub(/^Revert .*/, "R", s); printf "%s%s ", s, (n > 1 ? "(2)" : "") }' | sed 's/ $//')
out="$out$b=$line "
fi
done
staged=$(G diff --cached --name-only | wc -l | tr -d ' ')
changed=$(G diff --name-only | wc -l | tr -d ' ')
stash=$(G stash list | wc -l | tr -d ' ')
echo "$1: ${out}HEAD=$(G branch --show-current) staged=$staged changed=$changed stash=$stash"
}
fresh; edit; G add -A; say add
fresh; edit; G add -A; G commit -q -m C; say commit
fresh; edit; G stash -q; say stash
fresh; edit; G stash -q; G stash pop -q; say pop
fresh; G switch -q -c feature; say switchc
diverged feature; G switch -q main; say switch
diverged main; G merge -q --no-edit feature; say merge
diverged feature; G rebase -q main; say rebase
diverged main; G cherry-pick feature >/dev/null; say pick
three; G revert --no-edit HEAD >/dev/null; say revert
three; G reset -q --hard HEAD~1; say hard
three; G reset -q --soft HEAD~1; say soft
fresh; edit; echo staged > s.txt; G add s.txt; G status --short >/dev/null; say status
diverged main; G log --oneline --graph --all >/dev/null; say log
sh git.sh (needs git; runs in throwaway repositories, then deletes them)It printed:add: main=A B HEAD=main staged=1 changed=0 stash=0 commit: main=A B C HEAD=main staged=0 changed=0 stash=0 stash: main=A B HEAD=main staged=0 changed=0 stash=1 pop: main=A B HEAD=main staged=0 changed=1 stash=0 switchc: main=A B feature=A B HEAD=feature staged=0 changed=0 stash=0 switch: main=A B C feature=A B F1 HEAD=main staged=0 changed=0 stash=0 merge: main=A B C M(2) feature=A B F1 HEAD=main staged=0 changed=0 stash=0 rebase: main=A B C feature=A B C F1 HEAD=feature staged=0 changed=0 stash=0 pick: main=A B C F1 feature=A B F1 HEAD=main staged=0 changed=0 stash=0 revert: main=A B C R HEAD=main staged=0 changed=0 stash=0 hard: main=A B HEAD=main staged=0 changed=0 stash=0 soft: main=A B HEAD=main staged=1 changed=0 stash=0 status: main=A B HEAD=main staged=1 changed=1 stash=0 log: main=A B C feature=A B F1 HEAD=main staged=0 changed=0 stash=0
Each command on this sheet run in a fresh repository set up as its picture's "before"; each line is what the repository looked like after. The pictures are held to these lines by the site's tests.