Git Diff: Compare Two Recorded Snapshots
Compare endpoints, not a list of events
git diff OLD NEW compares the trees at two commits. OLD is the before side and NEW is the after side. It does not replay all intervening changes. In the clean chapter fixture HEAD~2 is the first commit and HEAD is the third.
The first comparison lists README.md as added and notes.txt as modified. The second limits the comparison to notes.txt; Rice is removed and Milk added. The separator -- distinguishes revision arguments from paths.
Direction matters
Swap the endpoints to ask how to get from the later snapshot to the earlier one. This is inspection, not an undo operation. The range-like notation A..B in git diff also compares these two endpoints; in git log it selects commits reachable from B but not A. Detailed graph ranges come later.
git diff HEAD HEAD~2 -- notes.txtgit diff HEAD~2..HEAD -- notes.txtgit log --oneline HEAD~2..HEADThe reversed patch removes Milk and adds Rice. The forward two-dot diff matches the forward two-endpoint diff; log lists the two commits after the first in this linear lab.
Recall working tree and index
Use the three-state model from Chapter 3. git diff compares index to working tree; git diff --cached compares HEAD to index; git diff HEAD compares HEAD to tracked working state. In a clean repository these are empty, even if recorded snapshots differ.
The first three produce no patch in the clean lab; the last reports two changed files. Untracked files are not included in ordinary working-tree diffs. A comparison requiring HEAD needs an existing commit.
Practice and common mistakes
Find which file was added after the initial snapshot and show its content at HEAD. Predict the result of comparing HEAD to itself: no patch. A change that was introduced and then completely reversed can disappear from the net diff even though both commits remain in history.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Diff: Compare Two Recorded Snapshots — knowledge check
Interpret Git history and differences using the chapter fixture.
Which command compares the first and third saved snapshots?
What happens when the two endpoints are swapped?
Why can git diff be empty even when earlier commits differ?
What does git log HEAD~2..HEAD select in the lab?
A change is fully reversed by a later commit. What can the net diff show?