Git cheatsheet
A one-page reference for Git. For the object model, DAG internals, and the full "Oh No" recovery playbook, see the complete guide.
๐ Full guide: Git โBranchingโ
git switch -c feature/x # create + switch
git switch main
git branch -d feature/x # delete (merged)
git branch -D feature/x # force delete
Merging vs rebasingโ
git merge feature/x # preserves history, adds merge commit
git rebase main # replays commits on top of main, linear history
Golden rule: never rebase commits already pushed/shared with others.
See: Merge vs rebase โ when to use eachInteractive rebaseโ
git rebase -i HEAD~4
# pick / reword / squash / fixup / drop
git rebase --continue
git rebase --abort
fetch vs pullโ
git fetch origin # downloads, doesn't merge
git pull origin main # fetch + merge (or --rebase)
git pull --rebase # avoids merge-bubble noise
Inspecting historyโ
git log --oneline --graph --all
git diff # working dir vs staged
git diff --staged # staged vs last commit
git show <commit>
Stashโ
git stash push -m "wip"
git stash list
git stash pop # apply + drop
git stash apply stash@{1} # apply, keep in list
Cherry-pick & bisectโ
git cherry-pick <sha> # apply one commit elsewhere
git bisect start
git bisect bad HEAD
git bisect good v1.2.0 # binary-searches the breaking commit
git bisect run npm test
Reflog โ your safety netโ
git reflog
git reset --hard HEAD@{2} # recover a "lost" commit/branch
Nothing reachable via reflog is truly gone, even after a hard reset.
Undoing thingsโ
git restore <file> # discard working-dir changes
git restore --staged <file> # unstage
git reset --soft HEAD~1 # undo commit, keep changes staged
git reset --hard HEAD~1 # undo commit, discard changes
git revert <sha> # new commit that undoes <sha> (safe on shared history)
reset rewrites history (local only); revert adds a new commit (safe to push).
Force-push, responsiblyโ
git push --force-with-lease # fails if remote has commits you haven't seen
Never plain --force on a shared branch โ --force-with-lease protects
against clobbering someone else's push.
Resolving conflictsโ
git status # lists conflicted files
# edit files, resolve <<<< ==== >>>> markers
git add <file>
git commit # or: git rebase --continue
Tags & .gitignoreโ
git tag v1.2.0 # lightweight
git tag -a v1.2.0 -m "release" # annotated (preferred for releases)
git push origin v1.2.0
.gitignore patterns are per-directory; git check-ignore -v <file> shows
which rule matched.