Comparing changes with diff

Topic 1 of 4 in Undoing changes6 min readBeginnerFree course

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:

Which changes does git diff show? Three boxes: working directory, staging area and last commit (HEAD). git diff compares the working directory with the staging area — unstaged changes. git diff –staged compares the staging area with the last commit — what will be committed. git diff HEAD compares the working directory with the last commit — everything since the last commit.

Terminal
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
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 milk
  • diff --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:

Terminal
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 main

The 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 --:

Terminal
git diff HEAD~3 HEAD -- src/cart.js
git diff main...feature -- docs/

Summaries and finer detail

Terminal
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:

Text
 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:

Text
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 difftool opens a configured graphical diff tool.
  • Pager tools like delta add 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 .gitattributes to mark generated files (package-lock.json -diff) so their huge diffs are summarised.

Habit: run git diff --staged right before every git commit. It’s the last chance to spot debug code, stray files or a missing change.

Exercise

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.

Terminal
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 only

Summary

  • git diff shows unstaged changes; git diff --staged shows what will be committed; git diff HEAD shows both.
  • In a unified diff, - lines are removed, + lines are added, and @@ headers say where each hunk is.
  • git diff A B compares any two commits; add -- path to limit it to files or folders.
  • git diff main...feature shows what a branch changed since it split from main.
  • --stat, --name-only and --word-diff give summaries or finer detail.