Articles
Published · November 18, 2026

How to Read a Unified Diff: Hunk Headers, Plus, and Minus Lines

A unified diff is easier to read once you know four things: the --- and +++ header, the @@ hunk header, the plus, minus, and space prefixes, and the context lines.

Also in: Español中文हिन्दी

Quick answer

Read a unified diff from the top down. The --- line names the old file and the +++ line names the new one. Each @@ -l,s +l,s @@ line starts a hunk, a block of changes, and says where it sits in each file. Inside a hunk, a line starting with - was removed, a line starting with + was added, and a line starting with a space is unchanged context. Wikipedia's diff article describes the format, and the rest of this guide walks through a real example.

Step-by-step

Here is a small diff of a notes file. It is the kind of output you get from git diff after editing a shopping list:

--- a/notes.md
+++ b/notes.md
@@ -1,4 +1,5 @@
 # Shopping
 - milk
-- eggs
+- free-range eggs
+- butter
 - bread

1. The lines above the first hunk

Git puts a diff --git a/notes.md b/notes.md line first, then an index line. Git's diff format reference says the index line includes the blob object names before and after the change. It also says the a/ and b/ filenames are the same unless a rename or copy is involved, so they are labels for the old and new side, not real folders.

Then come the two header lines. Per the unified format, the original file is preceded by --- and the new file by +++. Old first, new second. Keep that order in mind. It repeats in the hunk header.

2. The hunk header, piece by piece

The line @@ -1,4 +1,5 @@ follows the pattern @@ -l,s +l,s @@. In the words of the Wikipedia article, each range is l,s, where l is the starting line number and s is the number of lines the hunk applies to in that file. The minus range belongs to the old file and the plus range to the new one.

So -1,4 says: starting at line 1 of the old file, the hunk covers 4 lines. And +1,5 says: starting at line 1 of the new file, it covers 5 lines. The new side is one line longer, and the hunk body shows why.

Sometimes a range has no comma. In many versions of GNU diff a range can omit the comma and the count, and the count then defaults to 1. A header like @@ -7 +7 @@ means one line on each side, starting at line 7. You'll often see this after a single-line edit.

After the closing @@ there may be extra text. Git uses it to name the function or section the hunk falls under, as its format reference notes, which helps you find your place in a long code file.

3. Plus, minus, and space

Every line in the hunk body starts with one character. A space marks an unchanged context line, a plus marks an addition, and a minus marks a deletion. Git's own docs list the same characters, "normally +, - and ' ' respectively," in the description of its output indicator options.

In the example above, # Shopping, - milk, and - bread start with a space, so nothing changed there. The line -- eggs is the Markdown list item - eggs with a deletion minus in front of it, which looks odd the first time you see it. The two + lines are additions: +- free-range eggs and +- butter.

An edited line always appears as a pair: the old version with - and the new version with +. That is why one reworded line shows up as one deletion and one addition.

4. Check that the counts add up

The numbers in the header are checkable. The hunk range for the original is the sum of all context and deleted lines, and the range for the new file is the sum of all context and added lines.

| Side | Context lines | Changed lines | Total | Header says | | --- | --- | --- | --- | --- | | Old file | 3 | 1 deleted | 4 | -1,4 | | New file | 3 | 2 added | 5 | +1,5 |

Count the example and the numbers match, so you've read it correctly. When they don't, the diff was probably edited by hand or cut off.

5. Context lines

The unchanged lines around a change are there so you can see where it sits, and so a patch tool can find its place. Wikipedia notes that the number of unchanged lines above and below a change can be set by the user, even to zero, but three is typically the default. Git's documentation agrees: --unified=<n> generates diffs with n lines of context, and the number defaults to diff.context or 3 if that setting is unset. Run git diff -U1 for a tighter view or git diff -U10 to see more of the surroundings. See the git diff documentation for the full option list.

Common problems and fixes

"\ No newline at end of file". GNU diff and git print this line when the line above it has no final line break. It is a diagnostic, not part of your text, and Wikipedia notes the behavior is not portable across every diff tool. If it appears on a notes file, adding a newline at the end of the file usually removes it.

A long paragraph looks fully rewritten. In Markdown, a paragraph is often one long line, so changing a single word shows the whole line as one deletion and one addition. To see the exact word, compare by word instead, as covered in Compare Drafts by Word With git diff --word-diff.

You need to know when a line disappeared. A diff shows one change, not history. To find the commit that removed a line from your notes, search history with git log -S and -G.

You want to point someone at a spot. Instead of pasting a diff, you can share a line range of code with a GitHub permalink.

Doing this with Carets

A diff is plain text, and it only helps if the leading spaces, pluses, and minuses stay exactly as they are. Carets is a native notes and code editor for iPhone, iPad, and Mac that keeps Markdown, plain text, and code in one place. A diff saved as a plain text file stays readable there, and a fenced diff block in a Markdown note keeps an example next to your explanation. Related notes can be grouped into files, projects, and tags, so a diff you saved while reviewing a change stays with the rest of that project. Your files stay plain text, which keeps them portable. Get Carets on the App Store for iPhone, iPad, and Mac.

Frequently Asked Questions

What does @@ -1,4 +1,5 @@ mean in a diff?

The hunk starts at line 1 of the old file and covers 4 lines there, and starts at line 1 of the new file and covers 5 lines. The new side is one line longer because the hunk adds a line.

Why do some hunk headers have only one number?

In many versions of GNU diff a range can omit the comma and count, and the count then defaults to 1. So -7 +7 means one line on each side starting at line 7.

How many context lines does git diff show?

Three by default, unless the diff.context configuration variable is set. Use -U<n> to change it for one command.

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

GNU diff and git print it as a diagnostic when the line above it has no final line break. It is not portable across every diff tool.