Skip to main content

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.

CommandPurposeArea Touched
git initInitialize .git object database and unborn main branchLocal Repo
git statusInspect untracked, modified, staged, or conflicted pathsWorking Tree + Index
git add <path>Snapshot working file content into Staging IndexIndex
git rm --cached <path>Stop tracking file in Git without deleting local fileIndex
git mv <src> <dst>Move/rename tracked file and stage both halvesWorking Tree + Index
git commit -m "..."Record immutable commit pointing to staged treeLocal Repo

2. Inspecting History, Diffs & Authorship

CommandScope Compared
git diffWorking Tree vs Staging Index (unstaged edits only)
git diff --stagedStaging Index vs HEAD commit (ready to commit)
git log --oneline --graph -n 10Compact DAG topology with branch/tag pointers
git show HEAD~1Commit 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

CommandBehavior / Topology Effect
git switch -c feat/nameCreate and switch onto a new branch cleanly
git branch -m old newRename an existing branch without rewriting commits
git branch -d branchSafe delete: refuses if commits are still unmerged
git merge branchFast-forward if linear; 2-parent merge commit if diverged
git merge --no-ff branchAlways preserve 2-parent feature bubble in history
git merge --squash branchStage all branch changes into one flat patch (needs commit)
git merge --abortCancel 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.

CommandTrees ChangedHistory 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~1HEAD/branch only (keeps work staged)Local only (rewrites)
git reset --mixed HEAD~1HEAD/branch + Index (keeps Working Tree)Local only (rewrites)
git reset --hard HEAD~1HEAD + Index + Working Tree (destructive!)Local only (destructive)
git revert <sha>Appends inverse commit leaving history intactSafe (team standard)
git reflogLists every recent HEAD move to rescue lost SHAsRead-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/auth

    Fails 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

  1. Confirm `git status` shows zero unintended `.env`, build outputs, or conflict markers.
  2. Review `git diff --staged` before every commit so commits stay small and atomic.
  3. Verify current branch (`HEAD -> feat/...`) before starting urgent work or hotfixes.
  4. Prefer `git push --force-with-lease` (never bare `--force`) after rewriting a personal PR branch.
  5. Never rebase or amend commits already merged into shared `main`.