Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the numbers on a Git Diff header mean? [duplicate]

Tags:

diff

Every time I run git diff, for each single changes I made, I get some sort of header with numbers, for example:

@@ -169,14 +167,12 @@ function Browser(window, document, body, XHR, $log) {..... 

I wonder what does the four numbers mean? I guess -169 means that this particular line of code that follows was originally in line 169 but now is in 167? And what do 14 and 12 mean?

like image 446
Codier Avatar asked Jun 28 '11 15:06

Codier


People also ask

What do the numbers mean in git diff?

-1,6 means that this piece of the first file starts at line 1 and shows a total of 6 lines. Therefore it shows lines 1 to 6. 1 2 3 4 5 6. - means "old", as we usually invoke it as diff -u old new . +1,4 means that this piece of the second file starts at line 1 and shows a total of 4 lines.

What does ++ mean in git diff?

When viewing a combined diff, if the two files you're comparing have a line that's different from what they were merged into, you will see the ++ to represent: one line that was added does not appear in either file1 or file2.

What does git diff output mean?

Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


1 Answers

This header is called set of change, or hunk. Each hunk starts with a line that contains, enclosed in @@, the line or line range from,no-of-lines in the file before (with a -) and after (with a +) the changes. After that come the lines from the file. Lines starting with a - are deleted, lines starting with a + are added. Each line modified by the patch is surrounded with 3 lines of context before and after.

An addition looks like this:

@@ -75,6 +103,8 @@  foo  bar  baz +line1 +line2  more context  and more  and still context 

That means, in the original file before line 78 (= 75 + 3 lines of context) add two lines. These will be lines 106 (= 103 + 3 lines of context) through 107 after all changes.
Note the difference in from numbers (-75 vs +103), this means that there were other changes in this file before this particular hunk, that added 28 (103 - 75) lines of code.

A deletion looks like this:

@@ -75,7 +75,6 @@  foo  bar  baz -line1  more context  and more  and still context 

That means, delete line 78 (= 75 + 3 lines of context) in the original file. The unchanged context will be on lines 75 to 80 after all changes.
Note that from numbers in this hunk are equal (-75 and +75), this means that either there were no changes before this hunk, or amount of added and deleted lines in previous changes are the same.

Finally, a change looks like this:

@@ -70,7 +70,7 @@  foo  bar  baz -red +blue  more context  and more  still context 

That means, change line 73 (= 70 + 3 lines of context) in the file before all changes, which contains red to blue. The changed line is also line 73 (= 70 + 3 lines of context) in the file after all changes.

Credit goes to Markus Bertheau.

like image 181
Andrejs Cainikovs Avatar answered Sep 22 '22 20:09

Andrejs Cainikovs