Last Updated: May 1, 2025
Basic Diff Commands
git diffShow unstaged changes in working tree
git diff --stagedShow staged changes (about to be committed)
git diff HEADShow all changes (staged + unstaged) since last commit
git diff commit1 commit2Compare two commits
git diff branch1..branch2Compare the tips of two branches
git diff branch1...branch2Changes in branch2 since it diverged from branch1
git diff HEAD~3 HEADChanges over the last 3 commits
git diff --name-onlyShow only filenames that changed
Diff Options
git diff --word-diffInline word-level diff (great for text/docs)
git diff --color-wordsWord diff with color highlighting
git diff --statSummary of changed files with line counts
git diff --name-statusFiles changed with A/M/D (added/modified/deleted)
git diff -wIgnore whitespace changes
git diff --ignore-all-spaceIgnore ALL whitespace differences
git diff --diff-filter=MShow only modified files (A=added, D=deleted, R=renamed)
git diff -- filepathDiff only a specific file or directory
Advanced Diff Techniques
git diff --cachedAlias for --staged: show changes in the index
git diff HEAD^!Show diff for the last commit only
git diff --diff-algorithm=patienceBetter diff for badly-indented code
git diff --minimalSpend extra time finding smallest possible diff
git diff master...feature -- fileSymmetric diff filtered to one file
git diff --checkCheck for whitespace errors (trailing spaces, tabs)
git range-diff old..new other-old..other-newCompare two commit ranges (post-rebase)
git diff --output=changes.patchSave diff to a patch file
Real-World Diff Use Cases
| Item | Description |
|---|---|
Pre-Commit Review | git diff --staged → review everything about to be committed |
Compare Feature vs Main | git diff main...feature → see what a feature branch adds |
Rebase Verification | git range-diff before-rebase after-rebase → verify nothing lost |
Merge Preview | git diff main...feature → preview what a PR will change |
Hotfix Verification | git diff HEAD~1 → check what the last commit actually changed |
Pair Programming | git diff --word-diff → review documentation edits inline |
CI Check | git diff --check → enforce no trailing whitespace in CI |
Pro Tip: Use `git diff --word-diff` for documentation and prose — it highlights changed words inline instead of showing full line replacements.