Viewing changes (git diff)
git diff shows line-level differences between two trees — most often the working directory, the index, and HEAD — as a unified patch.
Why it matters
It is the review step before staging or committing: you see exactly what changed, catch stray edits and secrets, and confirm a fix is minimal. The same command compares branches, tags, and commits, so it is the engine behind every code review and PR diff.
How it works
The trick is knowing which two endpoints each invocation compares. By default git diff is working vs index — it deliberately hides already-staged changes.
| Command | Compares |
|---|---|
git diff | Working dir vs index (unstaged) |
git diff --staged | Index vs HEAD (what will commit) |
git diff HEAD | Working dir vs last commit (all changes) |
git diff A B | Commit/branch A vs B |
git diff A...B | B vs the merge-base of A and B |
- Reading a hunk —
@@ -12,6 +12,7 @@means: old file from line 12 spanning 6 lines, new from line 12 spanning 7;-lines removed,+added. - Word-level —
--word-diff(or--color-words) highlights intra-line changes, far clearer for prose/config than whole-line diffs. - Scope & noise control —
-- <path>limits to a file;-wignores whitespace-only changes;--statgives a summary instead of the patch. ...for PR review —git diff main...featureshows what the branch added since it forked, ignoring changes that landed onmainmeanwhile (matches what GitHub shows).
Example
git diff --stat # quick overview of unstaged edits
git add -p && git diff --staged # confirm ONLY the intended hunk is staged
git diff main...feature -- src/ # review a branch's src changes like the PR will
Pitfalls
- “diff shows nothing” after
git add— baregit diffexcludes staged changes; you needgit diff --staged(orgit diff HEAD). A..BvsA...B— two dots is a direct A→B comparison; three dots diffs against the merge base. Using the wrong one over-reports changes that came from the base branch.- Whitespace/CRLF storms — line-ending or reformat changes produce huge noisy diffs;
-wand a.gitattributesnormalization tame them. - Binary files — Git reports “Binary files differ” with no content; configure a textconv (e.g. for docx/png) to get a meaningful diff.