Read Git Diff: Understand a Unified Patch
What a patch describes
A patch describes differences between an old and a new version. It is not the entire new file. Use the clean three-commit history from the preceding lesson. Here the two older snapshots differ by one line.
Find the lines -Rice and +Milk. The leading minus and plus are patch markers, not part of the stored text. The unchanged Tea line has a leading space.
Read the header and hunk
A hunk is a group of nearby changed lines and their context. The excerpt below omits the object-ID line; it represents the actual two-line replacement. a/ and b/ normally label the old and new sides, not actual directory names.
In @@ -1,2 +1,2 @@, both sides start at line 1 and cover two lines. A space means unchanged context. The file headers --- and +++ are not deleted or added content. A replacement is represented as deletion plus addition.
Summaries and less context
Use --stat for an overview, --name-status for file names with change codes, and -U0 for zero unchanged context lines. These are alternate views of the same comparison.
git diff --stat HEAD~2 HEAD~1git diff --name-status HEAD~2 HEAD~1git diff -U0 HEAD~2 HEAD~1 -- notes.txtExpect one insertion and one deletion, M for notes.txt, and a hunk focused on the changed line. Rename detection is inferred from similarity, not stored as a rename event in commits; binary diffs may report a summary instead of text lines.
Practice and pitfalls
Predict the new file from the excerpt, then verify using git show HEAD~1:notes.txt. Compare the normal patch with git diff -w HEAD~2 HEAD~1 -- notes.txt (ignores whitespace for line comparison). Here Milk still differs from Rice. Do not use whitespace-ignoring output as proof that whitespace changes are harmless; indentation may be meaningful.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read Git Diff: Understand a Unified Patch — knowledge check
Interpret Git history and differences using the chapter fixture.
What does +Milk mean inside the illustrated hunk?
What does the leading space before Tea indicate?
In @@ -1,2 +1,2 @@ what does the second number on each side mean?
Which view lists filenames with change codes?
Why review normal diff even after using -w?