Git & Modern Collaboration
Git Command & Workflow Cheatsheet
Everyday commands, 3-tree undo matrix, branching topology, and emergency recovery. Print to PDF with Ctrl/Cmd + P.
PDF / Print
Save or print this cheatsheet as PDF
Print-ready layout: open in one click and save as PDF from your browser, no account or email required.
1. Four Areas & Core Snapshots
Git tracks immutable snapshots across Working Tree, Staging Index, Local Repo, and Remotes.
| Command | Purpose | Area Touched |
|---|---|---|
| git init | Initialize .git object database and unborn main branch | Local Repo |
| git status | Inspect untracked, modified, staged, or conflicted paths | Working Tree + Index |
| git add <path> | Snapshot working file content into Staging Index | Index |
| git rm --cached <path> | Stop tracking file in Git without deleting local file | Index |
| git mv <src> <dst> | Move/rename tracked file and stage both halves | Working Tree + Index |
| git commit -m "..." | Record immutable commit pointing to staged tree | Local Repo |
2. Inspecting History, Diffs & Authorship
| Command | Scope Compared |
|---|---|
| git diff | Working Tree vs Staging Index (unstaged edits only) |
| git diff --staged | Staging Index vs HEAD commit (ready to commit) |
| git log --oneline --graph -n 10 | Compact DAG topology with branch/tag pointers |
| git show HEAD~1 | Commit metadata + full diff of one ancestor commit |
| git blame <file> | Latest modifying commit SHA & author for each line |
| git tag -a v1.0.0 -m "..." | Attach immutable annotated release tag |
3. Branches & Merging Strategies
| Command | Behavior / Topology Effect |
|---|---|
| git switch -c feat/name | Create and switch onto a new branch cleanly |
| git branch -m old new | Rename an existing branch without rewriting commits |
| git branch -d branch | Safe delete: refuses if commits are still unmerged |
| git merge branch | Fast-forward if linear; 2-parent merge commit if diverged |
| git merge --no-ff branch | Always preserve 2-parent feature bubble in history |
| git merge --squash branch | Stage all branch changes into one flat patch (needs commit) |
| git merge --abort | Cancel conflicted merge and clean working directory |
4. Undo & Recovery Safety Matrix
Always match the command to which trees change — and never rewrite commits already shared with teammates.
| Command | Trees Changed | History Safe on Shared? |
|---|---|---|
| git restore <file> | Working Tree only (discards local edits) | Safe (uncommitted) |
| git restore --staged <file> | Index only (keeps Working Tree intact) | Safe |
| git reset --soft HEAD~1 | HEAD/branch only (keeps work staged) | Local only (rewrites) |
| git reset --mixed HEAD~1 | HEAD/branch + Index (keeps Working Tree) | Local only (rewrites) |
| git reset --hard HEAD~1 | HEAD + Index + Working Tree (destructive!) | Local only (destructive) |
| git revert <sha> | Appends inverse commit leaving history intact | Safe (team standard) |
| git reflog | Lists every recent HEAD move to rescue lost SHAs | Read-only recovery |
5. Team Collaboration & Debugging Recipes
Safe Force-Push After Local Rebase
git fetch origin git rebase origin/main git push --force-with-lease origin feat/authFails safely if a teammate pushed new commits you have not fetched yet.
Rescue a Commit Orphaned by reset --hard
git reflog git reset --hard HEAD@{1} # or anchor onto a fresh branch: git switch -c rescue HEAD@{1}Binary Search a Regression with Bisect
git bisect start HEAD v1.0.0 # test each midpoint: git bisect good # or git bisect bad git bisect reset # exit bisect back to original branch
6. Pre-Push Sanity Checklist
- Confirm `git status` shows zero unintended `.env`, build outputs, or conflict markers.
- Review `git diff --staged` before every commit so commits stay small and atomic.
- Verify current branch (`HEAD -> feat/...`) before starting urgent work or hotfixes.
- Prefer `git push --force-with-lease` (never bare `--force`) after rewriting a personal PR branch.
- Never rebase or amend commits already merged into shared `main`.