Progress:
TIER 1 · MODULE 07· Basics

git diff

Working tree, index, commits, branches — all comparable.

🎯 What & why

Show changes between two trees. With no arguments: working tree vs index. With one ref: working tree vs that ref. With two refs: between refs. With --cached: index vs HEAD. The same primitive backs every other 'what changed?' answer.

🧠 Mental model

Diff is a tree-to-tree operation. The 'tree' on either side may be HEAD's tree, the index, or your working tree. The output is a unified diff — a textual delta — but it's computed from object-database trees, not from text files. That's why git diff is fast on huge repos: it's comparing tree objects, not running diff(1) on every file.

🛠️ Synopsis

git diff [<options>] [<commit>] [--] [<path>...]
git diff [<options>] --cached [<commit>] [--] [<path>...]
git diff [<options>] <commit> <commit> [--] [<path>...]
git diff [<options>] <blob> <blob>

🎚️ Switches & options

FlagWhat it does
--cached, --stagedDiff index vs HEAD — what would git commit write?
--statShow file-level summary instead of full hunks.
--name-onlyOnly file names that changed.
--name-statusFile names plus an A/M/D letter.
-w, --ignore-all-spaceIgnore whitespace differences entirely.
-U<n>Lines of context around each hunk. Default 3.
--word-diffShow changes at the word level instead of line level.
--color-movedHighlight moved blocks in a different color. Surprisingly useful.
--minimalSpend more CPU to produce a smaller diff. For review.

💡 Use cases

🧪 Examples

Working tree vs index.
$ git diff
Index vs HEAD.
$ git diff --cached
Between two branches.
$ git diff main..feature
Between two branches via merge-base (= 'what's new on feature?').
$ git diff main...feature
Just the changed files, with status letters.
$ git diff --name-status HEAD~5

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.