Compare Staged and Unstaged Changes in Git
git diff compares the working tree with the index and shows unstaged changes, while git diff --staged compares the index with HEAD and shows exactly what the next commit will contain. Run both, or git diff HEAD for the combined view, when you need the full picture.
The short answer
git diff compares the working tree with the index: it shows changes you have made but not staged. git diff --staged (the same as --cached) compares the index with HEAD: it shows exactly what the next commit will contain. git diff HEAD compares the working tree with the last commit when you need both.
| Command | Compares | Answers |
|---|---|---|
git diff |
index to working tree | What have I not staged? |
git diff --staged |
HEAD to index |
What will be committed? |
git diff HEAD |
HEAD to working tree |
What has changed since the last commit? |
Git keeps three snapshots: the commit (HEAD), the index, and the working tree. A commit records the index, not the working tree, so the staged diff is the authoritative preview.
A minimal example
git init -q repo && cd repo
printf 'one\n' > a.txt
printf 'one\n' > b.txt
git add a.txt b.txt
git commit -qm initial
printf 'two\n' >> a.txt # changed, not staged
printf 'two\n' >> b.txt
git add b.txt # staged
printf 'three\n' >> b.txt # staged version, then changed again
git status --short
M a.txt
MM b.txt
The first column is index status, the second working-tree status. a.txt has only an unstaged modification. b.txt is staged and then modified again, so it appears in both diffs with different content:
git diff --stat # b.txt | 1 + (index "two" -> worktree "three")
git diff --staged --stat # b.txt | 1 + (HEAD "one" -> index "two")
Only --staged shows what git commit would save; the later edit to b.txt stays out until you stage it again.
Reading status columns
git status --short encodes the two comparisons in two characters. The first is the index against HEAD; the second is the working tree against the index.
| Code | Meaning |
|---|---|
M |
Modified in the working tree, not staged |
M |
Modified and staged |
MM |
Staged, then modified again |
A |
Newly added to the index |
D |
Deleted in the working tree, not staged |
?? |
Untracked file, absent from both diffs |
Why the index exists
The index is a real snapshot. Running git add hashes content into the object database and records that blob in the index. A later edit changes only the working-tree file; the staged blob is untouched, which is why one file can appear in both diffs.
git diff HEAD is useful for review but is not a commit preview: it includes unstaged edits. Use --staged whenever the question is specifically about the next commit.
Edge cases
Untracked files appear in neither diff. Add them with git add, or use git add -N PATH to record an intent-to-add entry so git diff shows their content.
Before the first commit, HEAD does not exist. git diff --staged still works against the empty tree, but git diff HEAD fails because HEAD cannot be resolved.
--cached is a synonym for --staged. Pathspecs work with all three forms, for example git diff --staged -- src/.
For scripts, git diff --quiet and --exit-code return 0 when there is no difference and 1 when there is. Combine either with --staged to test whether anything is staged. --name-status gives a compact added/modified/deleted list instead of hunks.
Practical checks
Before committing, run the staged diff and read it as the commit itself:
git status --short
git diff --staged --stat
git diff --staged
If the list contains files you did not expect, unstage them with git restore --staged PATH (or git reset -- PATH on older 2.x releases). An empty staged diff means the commit will be empty or will fail with nothing to commit.
When a commit is part of a release, the staged diff is the first checkpoint in verifying a static site release; deployed files should match what it describes.
Sources
Revision history
- Published
- Updated
- — Initial publication.