Comparing changes with diff
Overview
Before you commit, you should know exactly what you’re about to save. After a teammate pushes, you’ll want to know what they changed. git diff answers both. It’s one of the commands you’ll use most — and reading its output is a core skill for code review.
Which changes does git diff show?
Because Git has three areas, there are three useful comparisons:
git diff # working directory vs staging area: what you haven't staged yet
git diff --staged # staging area vs HEAD: what the next commit will contain
git diff HEAD # working directory vs HEAD: everything since the last commit(--cached is an older synonym for --staged.)
A common surprise: you edit a file, git add it, and git diff shows nothing. That’s correct — the change is staged, so there’s no difference between the working directory and the staging area. Use git diff --staged.
Untracked files never appear in git diff, because Git has nothing to compare them with. git status lists them.
Reading a unified diff
diff --git a/recipe.md b/recipe.md
index 3b18e51..a7c2f04 100644
--- a/recipe.md
+++ b/recipe.md
@@ -1,5 +1,5 @@
# Pancakes
- 2 eggs
-- 200 g flour
+- 250 g flour
- 300 ml milkdiff --git a/… b/…— the file being compared:a/is the old version,b/the new.index 3b18e51..a7c2f04— the blob IDs of the old and new versions.@@ -1,5 +1,5 @@— a hunk header: this hunk covers 5 lines starting at line 1 in the old file, and 5 lines starting at line 1 in the new one.- Lines starting with
-were removed, lines starting with+were added, and lines starting with a space are unchanged context (3 lines either side by default).
A modified line appears as a removal followed by an addition. (The recipe’s list items start with - themselves, which is why you see -- 200 g flour: the first - is the diff marker.)
Comparing commits and branches
git diff can compare any two commits:
git diff HEAD~1 HEAD # what the last commit changed (same as git show, minus the message)
git diff v1.0 v1.1 # everything between two releases
git diff main feature # the two branch tips
git diff main...feature # what feature changed since it branched off mainThe three-dot form is the one you usually want for a branch: it compares the branch with the point where it split from main, so it shows only the branch’s own changes, not everything that happened on main since. It’s exactly what a pull request on GitHub shows.
Limit any diff to some files with --:
git diff HEAD~3 HEAD -- src/cart.js
git diff main...feature -- docs/Summaries and finer detail
git diff --stat # files changed with +/- counts
git diff --name-only # just the file names
git diff --name-status # names with M/A/D/R codes
git diff --word-diff # changes within lines, word by word
git diff -w # ignore whitespace-only changes--stat output:
notes.md | 2 +-
recipe.md | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)--word-diff is excellent for prose and documentation, where a one-word change on a long line is hard to spot:
Serves 4 {+to 6+}Better diff tools
The terminal diff is fine for small changes. For bigger ones:
- Your editor: VS Code, JetBrains IDEs and others show side-by-side diffs and let you stage individual lines.
git difftoolopens a configured graphical diff tool.- Pager tools like
deltaadd syntax highlighting and side-by-side view to Git’s terminal output.
They all show the same comparisons; knowing which areas you’re comparing is what matters.
Making diffs easier to read
Diffs are only as readable as the changes behind them:
- Keep formatting changes (re-indenting, reordering imports) in separate commits from real changes, or the meaningful lines drown in noise.
- Configure an automatic formatter so everyone’s editor produces the same output.
- Use
.gitattributesto mark generated files (package-lock.json -diff) so their huge diffs are summarised.
Habit: run
git diff --stagedright before everygit commit. It’s the last chance to spot debug code, stray files or a missing change.
Review your own changes before committing
The script commits a small recipe, then edits it twice: one change is staged and another isn’t. Use git diff to show (1) only the unstaged change, (2) only the staged change, (3) everything since the last commit, (4) a summary of which files changed and by how much, and (5) a word-by-word diff of the unstaged change. Finally, compare the first commit with the current HEAD for recipe.md only.
mkdir kitchen && cd kitchen && git init -b main
printf "# Pancakes\n\n- 2 eggs\n- 200 g flour\n- 300 ml milk\n" > recipe.md
echo "Serves 4" > notes.md
git add . && git commit -qm "Add pancake recipe"
sed -i.bak 's/200 g flour/250 g flour/' recipe.md && rm recipe.md.bak
git add recipe.md
sed -i.bak 's/Serves 4/Serves 4 to 6/' notes.md && rm notes.md.bak
# 1. Unstaged changes
# 2. Staged changes
# 3. Everything since the last commit
# 4. Summary only
# 5. Word-level diff of the unstaged change
# 6. First commit vs HEAD, recipe.md onlymkdir kitchen && cd kitchen && git init -b main
printf "# Pancakes\n\n- 2 eggs\n- 200 g flour\n- 300 ml milk\n" > recipe.md
echo "Serves 4" > notes.md
git add . && git commit -qm "Add pancake recipe"
sed -i.bak 's/200 g flour/250 g flour/' recipe.md && rm recipe.md.bak
git add recipe.md
sed -i.bak 's/Serves 4/Serves 4 to 6/' notes.md && rm notes.md.bak
# 1. Unstaged changes → notes.md only
git diff
# 2. Staged changes → recipe.md only
git diff --staged
# 3. Everything since the last commit → both files
git diff HEAD
# 4. Summary only
git diff HEAD --stat
# 5. Word-level diff of the unstaged change
git diff --word-diff
# Serves 4 {+to 6+}
# 6. First commit vs HEAD, recipe.md only (the root commit, found with --max-parents=0)
first=$(git rev-list --max-parents=0 HEAD)
git commit -qm "Use more flour"
git diff "$first" HEAD -- recipe.mdSummary
git diffshows unstaged changes;git diff --stagedshows what will be committed;git diff HEADshows both.- In a unified diff,
-lines are removed,+lines are added, and@@headers say where each hunk is. git diff A Bcompares any two commits; add-- pathto limit it to files or folders.git diff main...featureshows what a branch changed since it split from main.--stat,--name-onlyand--word-diffgive summaries or finer detail.