How to Read a Diff: Unified Diff and git diff Output Explained
What the ---, +++ and @@ -12,7 +12,8 @@ lines in a diff mean, how to count hunk ranges, and what git adds on top of the unified diff format.
Every code review, patch file and git diff uses the same format, and most people learn to read it by squinting at green and red lines. That works until you need to know which line number a change is on, why a one-word edit shows up as two lines, or what \ No newline at end of file is complaining about. The format is small and fully regular, so it is worth learning properly once.
To follow along, paste two versions of any text into the tool below and switch it to the unified view. It runs in your browser, so neither version is uploaded.
Text DiffPaste two texts to see exactly what changed, line by line and word by word. Everything runs in your browser, so neither version is uploaded.A complete example
Here is a small change to a Python file, exactly as git diff prints it:
diff --git a/config.py b/config.py
index 3b18e51..a9c4f02 100644
--- a/config.py
+++ b/config.py
@@ -12,7 +12,8 @@ def load_config(path):
with open(path) as f:
data = json.load(f)
- timeout = data.get("timeout", 30)
+ timeout = data.get("timeout_seconds", 30)
+ retries = data.get("retries", 3)
host = data["host"]
port = data.get("port", 8080)
return Config(host, port, timeout)
It has three parts: a header naming the files, a hunk header starting with @@, and the hunk body. A diff of many files repeats the header for each file, and a file with changes in several places has several hunks.
The file header
--- a/config.py
+++ b/config.py
--- names the old version and +++ the new one. Every line in the body that starts with - belongs to the --- file, and every line starting with + belongs to the +++ file. That is the whole reason for the symbols.
The a/ and b/ prefixes are Git's convention for "before" and "after"; they are not real directories. Plain diff -u prints the actual paths, usually followed by modification timestamps. When a file is created, the old side is /dev/null, and when it is deleted, the new side is.
The hunk header
@@ -12,7 +12,8 @@ def load_config(path):
This is the line people skip, and it is the one that tells you where you are. Read it as two ranges:
-12,7means this hunk covers 7 lines of the old file, starting at line 12.+12,8means it covers 8 lines of the new file, starting at line 12.
The counts include the unchanged context lines, not just the edits. In the example, the old side is 6 context lines plus 1 removed line, which makes 7. The new side is the same 6 context lines plus 2 added lines, which makes 8. The new file grew by one line, which is exactly the difference between the two counts.
A few details complete the rule:
- A count of 1 is omitted.
@@ -5 +5 @@means one line on each side. - A count of 0 means that side is empty. A new file starts with
@@ -0,0 +1,20 @@: no old lines, 20 new ones. When a count is 0, the start number is the line after which the change happens rather than the line where it starts. - The two start numbers drift apart. Once an earlier hunk adds or removes lines, later hunks in the same file have different old and new start numbers. If the first hunk adds 3 lines, a later
@@ -80,6 +83,6 @@is the same place in the code, shifted by those 3 lines.
To find the line number of a specific + line, start from the new start number and count down through the hunk, counting context and + lines but skipping - lines. For a - line, do the same from the old start number, skipping + lines. In the example, the added retries line is line 16 of the new file.
The text after the second @@
def load_config(path): is not part of the change. It is a section heading: the nearest line above the hunk that looks like the start of a function or block, included so you can tell where you are without opening the file. Git guesses it with a simple rule by default (the nearest line starting with a letter, _ or $), which is why it is sometimes a comment or a variable. Setting a language driver in .gitattributes, such as *.py diff=python, makes the guess accurate.
The hunk body
Every body line starts with one character that says what it is:
| Prefix | Meaning |
|---|---|
| a space | Context: the line is the same in both versions |
- | The line exists only in the old version |
+ | The line exists only in the new version |
\ | A note about the line above, almost always \ No newline at end of file |
The prefix is an extra column, not part of the line. A context line indented by four spaces appears with five, and an empty context line appears as a single space. Some editors strip that trailing space when you save a patch file, which is one of the more common reasons a hand-edited patch refuses to apply.
There is no "changed" line
The format only knows removals and additions. A modified line is always shown as the old line removed followed by the new line added, so a one-word edit takes two lines of diff. Viewers that highlight the changed words inside such a pair are adding that on top; the diff itself does not record which words changed. In the terminal, git diff --word-diff or git diff --color-words shows the change inline instead.
Context lines
By default each hunk shows 3 unchanged lines before and after the changes. They exist so a patch can be applied to a file that has shifted slightly: the tool looks for the context to find the right place, rather than trusting the line numbers alone.
When two changes are close enough that their context would overlap, they are merged into one hunk. git diff -U10 shows 10 lines of context, and git diff -U0 shows none, which is compact but produces patches that are fragile to apply. git diff -W (--function-context) shows the whole enclosing function, which is often the most readable choice for review.
What Git adds on top
Plain diff -u output is only the file header and hunks. Git puts extra lines before the --- line:
diff --git a/config.py b/config.py
index 3b18e51..a9c4f02 100644
diff --git a/... b/...starts each file's section.index 3b18e51..a9c4f02 100644gives abbreviated blob hashes for the old and new contents, followed by the file mode.100644is a regular file and100755is an executable one.
Other header lines appear only when they apply:
new file mode 100644
deleted file mode 100644
old mode 100644
new mode 100755
similarity index 92%
rename from src/utils.py
rename to src/helpers.py
Binary files a/logo.png and b/logo.png differ
A file whose permissions changed but whose contents did not shows only the mode lines and no hunks. A renamed file shows similarity index and the rename lines, then hunks for whatever changed during the move. If rename detection is off, the same change appears as one whole file deleted and another added, which is much harder to review.
Reading a diff faster
- Read the hunk headers first. The section headings and line ranges give you the outline of the change before you read any code.
- Compare the counts.
-40,7 +40,25is a hunk that adds 18 lines;-40,25 +40,7removes them. You can tell additions from deletions before reading the body. - Ignore whitespace when the change is noisy.
git diff -whides indentation and spacing changes, which is useful after a reformat. Turn it back off before approving, because whitespace matters in Python, YAML and Makefiles. - Try another algorithm when a diff looks scrambled. If a hunk interleaves lines from two unrelated functions,
git diff --diff-algorithm=histogramoften lines them up the way you would expect. - Format structured data before comparing it. A single-line JSON payload is one line, so any change marks the whole thing as different. Pretty-print both sides first, or use a JSON-aware comparison that matches keys rather than lines.
Quick reference
| You see | It means |
|---|---|
--- a/file | The old version; its lines are marked - |
+++ b/file | The new version; its lines are marked + |
@@ -12,7 +12,8 @@ | 7 old lines from line 12, 8 new lines from line 12 |
@@ -0,0 +1,20 @@ | A new file with 20 lines |
Text after the second @@ | The nearest function or section heading, for orientation only |
A - line followed by a + line | One line modified |
\ No newline at end of file | The last line has no trailing line break |
/dev/null on one side | The file was created or deleted |
similarity index / rename from | The file was moved, possibly with edits |
