7

I been using git for years but never used diff command and I started to use it today but I really don't understand the output... I had a file and I removed line 3 and 4 and I got the following output but can someone help me understand what the lines of the output means in dummy terms. thanks

$ git diff
diff --git a/README.txt b/README.txt
index 15827f4..8115e72 100644
--- a/README.txt
+++ b/README.txt
@@ -1,4 +1,2 @@
 this file
 adding like
-line 3
-
isherwood
  • 58,414
  • 16
  • 114
  • 157
JMS
  • 183
  • 2
  • 9

2 Answers2

11

Line 1: The command used to produce the diff.

Line 2: git database information for the two files involved.

Line 3 and 4: --- means old file, +++ means new file.

Line 5: @@ means the range of lines represented in the next diff hunk. -1,4 means lines 1-4 from the old file, and +1,2 means lines 1-2 in the new file.

The remaining lines are lines from the original files, prefixed with either (a space), - or +. lines are in both old and new, - are only in old and + are only in new.

These are meant to be mnemonic: - are "removed lines", are "unchanged lines", and + are "added lines".

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 2
    Would be helpful to explain what 'old file' and 'new file' mean. Most examples and even git output use these 'a/xxx' and 'b/xxx' which is kind of confusing if/when your git command was something like 'git diff master origin/master' where no 'a or b' is in sight. – nyholku Jan 08 '19 at 15:56
  • I don't understand what is in line 2, the git database information. – thestarsatnight Aug 11 '22 at 17:13
0

It's just the unified diff format.

This is command line that produced this output, just to check any special flag:

diff --git a/README.txt b/README.txt

This is the hash of the index and the unix permission (0644) of the file

index 15827f4..8115e72 100644

This lines tell to what file the - and + you will see are referred:

--- a/README.txt
+++ b/README.txt

Here are not that useful, but if you applied diff to different file names (still remember this is a general format), it could be.

Follows the context, that is to say to which lines the diff is referred (for file -, line 1 to 4; for file +, line 1 to 2):

@@ -1,4 +1,2 @@

the context might also include the name of the function in which the change is located, so that diff can work even if you have further modified the file, as long as the point in which to apply the change can be located.

Finally, this is the changeset:

 this file
 adding like
-line 3
-

which means that you have removed those two lines, since they are in the - (old) file, but not in the new one (there is no + line).

user664833
  • 18,397
  • 19
  • 91
  • 140
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • 1
    Per https://en.wikipedia.org/wiki/Diff#Unified_format, the number after the comma is unrelated to columns, but accounts for the range (number of lines) affected by the change (removed or added) – Patrick Mevzek Oct 28 '22 at 20:52