Sub topic 5
Reading a unified diff
This chapter assumes you can commit, stage, and branch, and that you know what a commit is. Here you learn to read the text that git diff, git show, and git log -p print. Later chapters cover what git diff compares.
Why the format matters
Git prints every change as a unified diff. Code review tools, patch emails, and git apply all use the same format. Once you can read it, you can read changes from any source.
An annotated example
diff --git a/app.py b/app.py
index 3f2a1b0..8c4d9e2 100644
--- a/app.py
+++ b/app.py
@@ -10,4 +10,5 @@ def load():
path = find_path()
config = read(path)
- return config
+ validate(config)
+ return config
# end of loadThe output has two parts: a file header and one or more hunks. A hunk is a contiguous block of changes with surrounding context.
The file header
diff --git a/app.py b/app.pynames the old path (a/) and the new path (b/). Thea/andb/prefixes are labels for the two sides. They are not real directories.index 3f2a1b0..8c4d9e2 100644gives the abbreviated object IDs of the old and new file contents, then the file mode.100644is a regular file.--- a/app.pyand+++ b/app.pyrepeat the two sides. For a new file, the old side is/dev/null. For a deleted file, the new side is/dev/null.
The hunk header
@@ -10,4 +10,5 @@ def load(): reads as follows:
| Part | Meaning |
|---|---|
| `-10,4` | In the old file, the hunk starts at line 10 and covers 4 lines |
| `+10,5` | In the new file, the hunk starts at line 10 and covers 5 lines |
| `def load():` | A hint: the nearest preceding line that looks like a function heading |
The hint helps you locate the change without counting lines. It is not part of the file's content at that spot.
The body lines
The first character of each line tells you its role:
- A space means the line is unchanged context.
-means the line was removed from the old version.+means the line was added in the new version.
The old side of the hunk is the context lines plus the - lines: 2 + 1 + 1 = 4. The new side is the context lines plus the + lines: 2 + 2 + 1 = 5. This matches -10,4 +10,5.
A modified line appears as a - line followed by a + line. In the example, return config is removed and then added again below the new validate(config) line. Git matches lines and chose to show it this way, so the original line looks rewritten even though only one line is genuinely new.
@@ -10,4 +10,5 @@ def load():Seeing the same structure
A file with changes in distant places gets several hunks, each with its own @@ header.
Common mistakes
- Reading
-10,4as "lines 10 to 4". The second number is a line count. - Treating
+as good and-as bad. They only mark direction from old to new. - Ignoring
\ No newline at end of file. This line means the file lacks a final newline on that side, which can make an otherwise identical last line appear changed. - Forgetting that the order matters. Swapping the two sides reverses every
+and-.
Exercise
A hunk header reads @@ -42,6 +42,8 @@. The body has 4 context lines and 2 removed lines. How many lines were added?
Show answer
4 lines were added. The old side has 6 lines, which is 4 context plus 2 removed. The new side has 8 lines, so 8 minus 4 context lines leaves 4 added lines.
Exercise
In a diff, what do --- /dev/null and +++ b/notes.txt together tell you?
Show answer
The file notes.txt is new. It had no old version, so the old side is /dev/null.