Cheat sheetGit
Git
Daily commands, branching, undo patterns, and interview-safe explanations.
Everyday
bash
git status
git diff
git diff --staged
git add -p # stage hunks
git commit -m "message"
git log --oneline --graph -20
git switch branch-name
git switch -c feature/xSync
bash
git fetch origin
git pull # fetch + merge (or rebase if configured)
git pull --rebase
git push -u origin HEAD
git push --force-with-lease # safer than --forceUndo (choose carefully)
| git restore file | Discard unstaged work in file |
|---|---|
| git restore --staged | Unstage, keep changes |
| git reset --soft HEAD~1 | Undo commit, keep staged |
| git reset HEAD~1 | Undo commit, keep unstaged |
| git revert <sha> | New commit that undoes a commit (safe for shared) |
Rebase & stash
bash
git fetch origin
git rebase origin/main
git rebase -i HEAD~3 # squash/edit locally
git stash -u
git stash popInterview tips
- ✓Explain merge vs rebase: history shape vs linear story; never rewrite shared main.
- ✓Prefer --force-with-lease over --force.
- ✓revert for public history; reset for local only.
Common mistakes
- !Force-pushing rewritten main/master.
- !Huge unrelated commits — reviewers hate it.
- !Committing secrets; rotate keys immediately if you do.