Git
diff
version control
code comparison
patch

What does -1 1 mean in Git's diff output?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When Git prints a line like @@ -1 +1 @@, it is describing where a change appears in the old file and the new file. That line is called a hunk header, and once you understand its structure, the rest of unified diff output becomes much easier to read.

The Shape of a Hunk Header

Git diff output is split into hunks, where each hunk is one contiguous block of changed lines plus some surrounding context. A typical hunk header looks like this:

text
@@ -start_old,count_old +start_new,count_new @@

The minus side refers to the original file. The plus side refers to the new file.

So in:

text
@@ -1 +1 @@

Git is saying:

  • In the old file, this hunk starts at line 1
  • In the new file, this hunk also starts at line 1

The counts are omitted because each side covers exactly one line. In other words, @@ -1 +1 @@ is shorthand for:

text
@@ -1,1 +1,1 @@

Git suppresses ,1 because it would be redundant.

A Small Example

Suppose a file originally contains:

text
Hello, world!

You change it to:

text
Hello, Git!

Running git diff might show:

diff
1diff --git a/hello.txt b/hello.txt
2index 802992c..8ae06cb 100644
3--- a/hello.txt
4+++ b/hello.txt
5@@ -1 +1 @@
6-Hello, world!
7+Hello, Git!

Read the hunk header together with the changed lines:

  • '-Hello, world! means this line existed in the old file'
  • '+Hello, Git! means this line exists in the new file'
  • '@@ -1 +1 @@ says that both versions of the changed line are at line 1'

The header is about line positions, not the text that changed.

When Counts Appear Explicitly

Counts appear whenever the hunk involves something other than exactly one line on a side. For example:

text
@@ -4,3 +4,5 @@

This means:

  • The old hunk starts at line 4 and covers 3 lines
  • The new hunk starts at line 4 and covers 5 lines

That usually indicates inserted or deleted lines changed the size of the block.

Here is a concrete case:

diff
1@@ -4,2 +4,3 @@
2 line four
3-line five
4+line five updated
5+line six inserted

The old side covered two lines beginning at line 4. The new side covers three lines beginning at line 4, because one additional line was inserted.

Why Line Numbers Sometimes Look Strange

Hunk headers do not identify every single changed line independently. They identify the starting point and span of the hunk after Git groups nearby edits together. Because Git includes surrounding context lines, a change on line 10 may appear in a hunk whose header starts on line 8.

You can reduce context and make the hunk tighter with:

bash
git diff --unified=0

That often produces smaller hunks and makes the line arithmetic easier to inspect when debugging patches.

How This Relates to Patches

Unified diff is not just for humans. Patch tools also use the hunk header to know where to apply a change. The header tells the patching tool, "look near this position in the old file and replace a block of this size with a new block of that size."

That is why line counts matter. If the counts are wrong, patch application becomes unreliable. Git generates them for you, but understanding them helps when you read patch failures or merge conflicts.

Deletions and Insertions at File Boundaries

Special cases often confuse new users:

  • '@@ -1,0 +1,3 @@ means three lines were inserted starting at line 1 in the new file'
  • '@@ -7,2 +7,0 @@ means two lines were deleted starting at line 7 in the old file'

The side with count 0 indicates there were no lines there in that version of the file.

A Fast Way to Read Diff Output

When scanning a diff, use this order:

  1. Read the file header lines beginning with --- and +++.
  2. Read the hunk header beginning with @@.
  3. Read context lines that begin with a space.
  4. Read removed lines beginning with -.
  5. Read added lines beginning with +.

That sequence helps you avoid a common mistake: interpreting the hunk header as a count of changed lines only. It actually describes the whole hunk span, including context shown in the patch.

Common Pitfalls

The biggest pitfall is thinking -1 means "delete one line" and +1 means "add one line." In the hunk header, those numbers are starting line numbers, not commands.

Another mistake is forgetting that ,1 can be omitted. So @@ -1 +1 @@ and @@ -1,1 +1,1 @@ mean the same thing.

People also confuse the file header lines --- and +++ with the actual removed and added lines inside the hunk. The file header identifies the old and new file versions; single - and + lines inside the hunk are the content changes.

Finally, remember that hunk headers describe grouped changes with context. If the header starts a few lines before the exact edit, that is normal.

Summary

  • '@@ -1 +1 @@ is a unified diff hunk header.'
  • The minus side refers to the old file and the plus side refers to the new file.
  • '-1 and +1 are starting line numbers, not instructions.'
  • Missing counts imply a span of exactly one line on that side.
  • Understanding hunk headers makes patch files, merge conflicts, and git diff output much easier to read.

Course illustration
Course illustration

All Rights Reserved.