How to Read a Diff

Updated · 14 min read
easyGit 2.53gitdiffunified-diffhunkinterview

A road map is full of symbols: a blue line for a river, a small cross for a hospital, a dotted line for a border. At first it looks crowded. Once you know the symbols, you can read a whole city at a glance.

A diff is the same. git diff, git show, git log -p and every pull request on GitHub, GitLab and Bitbucket use one format, called the unified diff. It looks crowded the first time, but it uses only a handful of symbols. Once you know them, you can review a change in seconds and spot a mistake before it is committed.

In this lesson, you will read a real diff line by line, learn what the numbers in @@ mean, and recognize the special cases: new, deleted, renamed and binary files.

A Diff with Two Changes

Navika's Java class has fifteen lines:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
    static final int TERM_FEE = 12000;
    static final int LATE_FEE = 250;
 
    int total(int terms) {
        return terms * TERM_FEE;
    }
 
    int lateFee(int daysLate) {
        if (daysLate <= 0) {
            return 0;
        }
        return LATE_FEE;
    }
}
EOF
git add FeeCalculator.java
git commit --quiet -m "Add the fee calculator"
cat -n FeeCalculator.java
Output
     1	class FeeCalculator {
     2	    static final int TERM_FEE = 12000;
     3	    static final int LATE_FEE = 250;
     4
     5	    int total(int terms) {
     6	        return terms * TERM_FEE;
     7	    }
     8
     9	    int lateFee(int daysLate) {
    10	        if (daysLate <= 0) {
    11	            return 0;
    12	        }
    13	        return LATE_FEE;
    14	    }
    15	}

cat -n prints the file with line numbers, which you will need in a moment. Now she makes two changes far apart: the late fee goes up to $300 on line 3, and on line 13 it is charged for every day late:

Terminal
sed -i.bak -e "s/LATE_FEE = 250/LATE_FEE = 300/" -e "s/return LATE_FEE;/return LATE_FEE * daysLate;/" FeeCalculator.java
rm FeeCalculator.java.bak
git diff
Output
diff --git a/FeeCalculator.java b/FeeCalculator.java
index 9a9163f..76b5796 100644
--- a/FeeCalculator.java
+++ b/FeeCalculator.java
@@ -1,6 +1,6 @@
 class FeeCalculator {
     static final int TERM_FEE = 12000;
-    static final int LATE_FEE = 250;
+    static final int LATE_FEE = 300;
 
     int total(int terms) {
         return terms * TERM_FEE;
@@ -10,6 +10,6 @@ class FeeCalculator {
         if (daysLate <= 0) {
             return 0;
         }
-        return LATE_FEE;
+        return LATE_FEE * daysLate;
     }
 }
The parts of a unified diff

The File Header

The first four lines say which file this section is about.

LineMeaning
diff --git a/FeeCalculator.java b/FeeCalculator.javaA new file section starts. a/ is the old version, b/ the new one.
index 9a9163f..76b5796 100644The short IDs of the old and new content, then the file mode. 100644 is an ordinary file.
--- a/FeeCalculator.javaThe old file. Its lines are marked with - below.
+++ b/FeeCalculator.javaThe new file. Its lines are marked with + below.

When a diff covers several files, each file gets its own header and its own hunks.

Hunks and the @@ Line

A hunk is one block of changes with some unchanged lines around it. This diff has two hunks, because the two changes are far apart. Each hunk starts with a header between @@ signs:

text
@@ -1,6 +1,6 @@
PartMeaning
-1,6In the old file, this hunk starts at line 1 and covers 6 lines
+1,6In the new file, it starts at line 1 and covers 6 lines

The second hunk is @@ -10,6 +10,6 @@: it shows lines 10 to 15 of both versions. Compare it with the cat -n output above, and you will see that the hunk really starts at if (daysLate <= 0) { on line 10.

The two numbers are the same here because each change replaced one line with one line. When a change adds lines, the new count is larger. If you add two lines in the first hunk, it becomes @@ -1,6 +1,8 @@, and every later hunk starts two lines further down in the new file, for example @@ -10,6 +12,6 @@.

The text after the second @@ is a hint about where you are in the file. Here it says class FeeCalculator {. "Under the Hood" explains where it comes from and how to make it name the method instead.

Inside a Hunk

Every line inside a hunk starts with one character:

First characterMeaning
a spaceContext: the line is the same in both versions
-The line was removed. It is only in the old version.
+The line was added. It is only in the new version.

There is no symbol for "changed". A changed line is always shown as the old line with - followed by the new line with +, as on line 3 here.

By default, Git shows three lines of context before and after each change. The first change is on line 3, so the hunk starts at line 1 (there is nothing before it) and ends at line 6. The second change is on line 13, so the hunk shows lines 10 to 15 (and the file ends at 15).

More or Less Context: -U

-U<n> (unified, with n lines of context) changes the number of context lines. With more context, the two hunks overlap and Git joins them into one:

Terminal
git diff -U5
Output
diff --git a/FeeCalculator.java b/FeeCalculator.java
index 9a9163f..76b5796 100644
--- a/FeeCalculator.java
+++ b/FeeCalculator.java
@@ -1,15 +1,15 @@
 class FeeCalculator {
     static final int TERM_FEE = 12000;
-    static final int LATE_FEE = 250;
+    static final int LATE_FEE = 300;
 
     int total(int terms) {
         return terms * TERM_FEE;
     }
 
     int lateFee(int daysLate) {
         if (daysLate <= 0) {
             return 0;
         }
-        return LATE_FEE;
+        return LATE_FEE * daysLate;
     }
 }

With -U0, there is no context at all, only the changed lines:

Terminal
git diff -U0
Output
diff --git a/FeeCalculator.java b/FeeCalculator.java
index 9a9163f..76b5796 100644
--- a/FeeCalculator.java
+++ b/FeeCalculator.java
@@ -3 +3 @@ class FeeCalculator {
-    static final int LATE_FEE = 250;
+    static final int LATE_FEE = 300;
@@ -13 +13 @@ class FeeCalculator {
-        return LATE_FEE;
+        return LATE_FEE * daysLate;

@@ -3 +3 @@ is short for -3,1 +3,1: one line at line 3. A count of 1 is left out.

New and Deleted Files

Navika commits the change, adds a README and deletes a file of notes:

Terminal
echo "Fee notes" > notes.txt
git add notes.txt
git commit --quiet -am "Charge the late fee per day and add notes"
echo "Fee Tracker" > README.md
git add README.md
git rm --quiet notes.txt
git diff --staged
Output
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d5924b0
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+Fee Tracker
diff --git a/notes.txt b/notes.txt
deleted file mode 100644
index 8e53b3b..0000000
--- a/notes.txt
+++ /dev/null
@@ -1 +0,0 @@
-Fee notes
SignMeaning
new file mode 100644 and --- /dev/nullThe file didn't exist before. /dev/null means "no file". Every line is +, and the hunk starts with -0,0.
deleted file mode 100644 and +++ /dev/nullThe file doesn't exist after. Every line is -, and the hunk ends with +0,0.

Renamed Files and Mode Changes

Terminal
git commit --quiet -m "Add README, remove notes"
git mv FeeCalculator.java Fees.java
chmod +x README.md
git add README.md
git diff --staged
Output
diff --git a/FeeCalculator.java b/Fees.java
similarity index 100%
rename from FeeCalculator.java
rename to Fees.java
diff --git a/README.md b/README.md
old mode 100644
new mode 100755

A rename with no content change has no hunks at all, only similarity index 100%, rename from and rename to. If the content had also changed a little, the similarity would be lower, and the changed lines would follow as usual. old mode and new mode say that the file's permissions changed: 100755 means the file can be run as a program.

No Newline at End of File

A text file should end with a new line character. If the last line has none, Git says so:

Terminal
git commit --quiet -m "Rename the calculator and make README runnable"
printf "Fee Tracker\nTracks school fees." > README.md
git diff
Output
diff --git a/README.md b/README.md
index d5924b0..95ceec3 100755
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
 Fee Tracker
+Tracks school fees.
\ No newline at end of file

\ No newline at end of file is not part of the file. It is a note about the line just above it. Many editors add the final new line for you, which is why you sometimes see a diff that "changed nothing" except this note.

Binary Files

Git can't show lines for a file that isn't text, such as an image. It only says that the file changed:

Terminal
printf "\x89PNG\x00\x01" > logo.png
git add logo.png
git diff --staged -- logo.png
Output
diff --git a/logo.png b/logo.png
new file mode 100644
index 0000000..f584f40
Binary files /dev/null and b/logo.png differ

Common Mistakes

Reading - and + as "bad" and "good". They only mean "old" and "new". A - line in a diff that fixes a bug is the buggy line, which is exactly what you want to remove.

Expecting a "changed" marker. A changed line is always a removal plus an addition.

Taking the hunk numbers as the changed line. @@ -10,6 +10,6 @@ is where the hunk starts, including context. The change itself is three lines further down.

Treating \ No newline at end of file as file content. It is Git's note. Fix it by ending the file with a new line, which most editors can do automatically.

Missing the rename because there are no + or - lines. A pure rename or a mode change has a header and no hunks.

Under the Hood

The text after the second @@ is called the hunk header context or function name. Git looks upward from the start of the hunk for the nearest line that looks like the start of a function. By default, that is any line that starts with a letter, _ or $, with no spaces in front. In a Java file, methods are indented, so the default finds only class FeeCalculator {.

Git also has built-in rules for many languages, including Java. You turn them on in a file called .gitattributes:

Terminal
git checkout --quiet -- README.md
git rm --quiet --cached logo.png
rm logo.png
echo "*.java diff=java" > .gitattributes
sed -i.bak "s/return LATE_FEE \* daysLate;/return Math.min(LATE_FEE * daysLate, 3000);/" Fees.java
rm Fees.java.bak
git diff -- Fees.java
Output
diff --git a/Fees.java b/Fees.java
index 76b5796..a1fbab8 100644
--- a/Fees.java
+++ b/Fees.java
@@ -10,6 +10,6 @@ int lateFee(int daysLate) {
         if (daysLate <= 0) {
             return 0;
         }
-        return LATE_FEE * daysLate;
+        return Math.min(LATE_FEE * daysLate, 3000);
     }
 }

Now the hunk header names the method, int lateFee(int daysLate) {, which tells a reviewer at once where the change is. Commit the .gitattributes file, and everyone on the team gets the same headers. Rules exist for C, C#, Go, Python, Kotlin, Markdown and others too.

Interview Corner

What does @@ -10,6 +10,7 @@ mean in a diff?

It is a hunk header. In the old file, the hunk starts at line 10 and covers 6 lines. In the new file, it starts at line 10 and covers 7 lines. So the hunk added one line overall. The line numbers include the context lines around the change.

How does a unified diff show a changed line?

As a removal followed by an addition: the old line starts with - and the new line starts with +. There is no separate "changed" marker.

What are context lines, and how do you change how many are shown?

Unchanged lines shown around a change, starting with a space, so the reader can see where the change is. Git shows three by default. -U<n> sets the number, for example git diff -U0 for none.

What does /dev/null mean in a diff?

That there is no file on that side. --- /dev/null means the file is new, and +++ /dev/null means it was deleted.

What does "\ No newline at end of file" mean?

The line just above it is the last line of the file and doesn't end with a new line character. It is a note from Git, not part of the file.

Practice

Challenge: predict the hunk header. Continue in the same repository. Commit the current change, then add a new line static final int EXAM_FEE = 800; directly after the LATE_FEE line (line 3) of Fees.java. Show the diff with one line of context.

What does Git print? Before you open the solution, write down the @@ line: where the hunk starts and how many lines it covers in the old and the new file.

Show solution
Terminal
git add .gitattributes
git commit --quiet -am 'Cap the late fee at $3,000 and use Java diff headers'
sed -i.bak "3a\\
    static final int EXAM_FEE = 800;
" Fees.java
rm Fees.java.bak
git diff -U1
Output
diff --git a/Fees.java b/Fees.java
index a1fbab8..4a13296 100644
--- a/Fees.java
+++ b/Fees.java
@@ -3,2 +3,3 @@ class FeeCalculator {
     static final int LATE_FEE = 300;
+    static final int EXAM_FEE = 800;

With one line of context, the hunk shows line 3 before the addition and line 4 after it. Line 4 is the empty line, so the hunk ends with a context line that looks blank. In the old file, that is 2 lines starting at line 3: -3,2. In the new file, it is 3 lines, because one was added: +3,3. The text after @@ is class FeeCalculator {, because the new line is inside the class but not inside a method.

Checkpoint. Open a recent pull request in any project, or run git log -p -3 in your own. For one hunk, check the numbers in the @@ line against the file, and explain every line that starts with - or +.

Quick check: Reading diffs

4 questions
1In a diff, what does a line starting with a space mean?
2In @@ -3,2 +3,3 @@, how many lines does the new file's part of the hunk have?
3How is a changed line shown?
4A diff shows --- /dev/null. What happened to the file?

Key Takeaways

Quick reference

You seeIt means
diff --git a/X b/XThe changes to file X start here
--- a/X / +++ b/XThe old and the new version
@@ -10,6 +10,7 @@Hunk: old lines 10 to 15, new lines 10 to 16
(a space)Unchanged context line
-Line only in the old version
+Line only in the new version
/dev/nullNo file on that side: new or deleted
similarity index, rename from, rename toA renamed file
old mode / new modePermissions changed
\ No newline at end of fileThe line above has no final new line
Binary files ... differA non-text file changed
git diff -U<n>Show n context lines

Was this tutorial helpful?

Lesson 20 of 106