Git Diff Cheat Sheet

Git diff — compare commits, branches, staged changes, working tree. Word diff, diff filters, range diff, and practical diff workflows.

Last Updated: May 1, 2025

Basic Diff Commands

git diff
Show unstaged changes in working tree
git diff --staged
Show staged changes (about to be committed)
git diff HEAD
Show all changes (staged + unstaged) since last commit
git diff commit1 commit2
Compare two commits
git diff branch1..branch2
Compare the tips of two branches
git diff branch1...branch2
Changes in branch2 since it diverged from branch1
git diff HEAD~3 HEAD
Changes over the last 3 commits
git diff --name-only
Show only filenames that changed

Diff Options

git diff --word-diff
Inline word-level diff (great for text/docs)
git diff --color-words
Word diff with color highlighting
git diff --stat
Summary of changed files with line counts
git diff --name-status
Files changed with A/M/D (added/modified/deleted)
git diff -w
Ignore whitespace changes
git diff --ignore-all-space
Ignore ALL whitespace differences
git diff --diff-filter=M
Show only modified files (A=added, D=deleted, R=renamed)
git diff -- filepath
Diff only a specific file or directory

Advanced Diff Techniques

git diff --cached
Alias for --staged: show changes in the index
git diff HEAD^!
Show diff for the last commit only
git diff --diff-algorithm=patience
Better diff for badly-indented code
git diff --minimal
Spend extra time finding smallest possible diff
git diff master...feature -- file
Symmetric diff filtered to one file
git diff --check
Check for whitespace errors (trailing spaces, tabs)
git range-diff old..new other-old..other-new
Compare two commit ranges (post-rebase)
git diff --output=changes.patch
Save diff to a patch file

Real-World Diff Use Cases

ItemDescription
Pre-Commit Reviewgit diff --staged → review everything about to be committed
Compare Feature vs Maingit diff main...feature → see what a feature branch adds
Rebase Verificationgit range-diff before-rebase after-rebase → verify nothing lost
Merge Previewgit diff main...feature → preview what a PR will change
Hotfix Verificationgit diff HEAD~1 → check what the last commit actually changed
Pair Programminggit diff --word-diff → review documentation edits inline
CI Checkgit 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.