Sub topic 6
What you are comparing: working tree, index, HEAD, and commits
This chapter assumes you can init or clone a repository, stage files with git add, commit, and create branches. It also assumes you can read a unified diff, which the previous chapter covered. Here the question is which two things git diff compares.
The three places your changes live
Git tracks your files in three places at once.
- The working tree is the files on disk that you edit.
- The index (also called the staging area) holds the snapshot that your next commit will contain.
git addcopies file contents from the working tree into it. - HEAD is the commit you currently have checked out. It is usually the tip of your current branch.
Every git diff form compares two of these, or two commits.
The common comparisons
git diff # working tree vs index (unstaged changes)
git diff --staged # index vs HEAD (what the next commit will contain)
git diff HEAD # working tree vs HEAD (all uncommitted changes)
git diff HEAD~1 HEAD # the last commit against its parent--cached is a synonym for --staged. In git diff HEAD~1 HEAD, the first argument is the "old" side and the second is the "new" side. Swapping them reverses every + and -.
A worked example
Suppose app.py is committed. You edit it, stage the edit, and then edit it again.
echo "# first edit" >> app.py
git add app.py
echo "# second edit" >> app.py
git diff # shows only "# second edit"
git diff --staged # shows only "# first edit"
git diff HEAD # shows both editsThe working tree differs from the index by the second edit. The index differs from HEAD by the first edit. git diff HEAD spans both steps.
Untracked files
git diff only looks at files Git already tracks. A brand-new file does not appear in git diff until you stage it. Once you run git add, it shows up in git diff --staged. To include it in a comparison without committing, you can use git add -N <file> (intent to add), which makes it appear as a new file in plain git diff.
Comparing commits and paths
Any argument that names a commit works: a hash, a branch, a tag, or a relative name like HEAD~3.
git diff v1.0 v1.1 # between two tags
git diff main feature # tip of main against tip of feature
git diff HEAD~3 -- src/ # working tree vs three commits ago, limited to src/The -- separates revisions from paths. It avoids confusion when a file and a branch share a name.
With one commit and no second commit, Git compares that commit with your working tree. This is why git diff HEAD includes unstaged changes.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Empty `git diff` after editing | The changes are already staged | Use `git diff --staged` |
| Diff looks reversed | Arguments are in the wrong order | Put the older commit first |
| New file missing from the diff | The file is untracked | `git add` it, or use `git add -N` |
| "ambiguous argument" error | A path was read as a revision | Add `--` before the path |
Exercise
You edit config.py, run git add config.py, then edit config.py again. Which command shows only the first edit, and which shows both edits?
Show answer
git diff --staged shows only the first edit, because it compares the index with HEAD. git diff HEAD shows both edits, because it compares the working tree with HEAD.
Exercise
You run git diff and get no output, but git status lists a modified file. What is the most likely explanation, and how do you see the change?
Show answer
The modification is staged, so the working tree and index are identical. Run git diff --staged to see it.