N08
N08

Reputation: 1315

Making sense of git diff

I have a file "file.txt" in my repo which contains three lines:

1
2
3

This file has been committed. However, if I now add a fourth line to the file, it looks like this:

1
2
3
4

When I enter "git diff file.txt" it gives me the following output:

diff --git a/file.txt b/file.txt
index 5f5fbe7..b178657 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
 1
 2
-3
\ No newline at end of file
+3
+4
\ No newline at end of file

In other words, at row 1 three lines have been removed and 4 have been added according to git diff (@@ -1,3 +1,4 @@). This doesn't make sense to me, as all I did was to add a fourth line - so I would expect the output (-0,0 +4,1)

What is happening here?


I am running git on Windows 7.

Upvotes: 0

Views: 272

Answers (3)

Amadan
Amadan

Reputation: 198294

UNIX defines a line as "string of characters ending in a line terminator". Thus, almost all UNIX-based editors will have lines like this:

1\n
2\n
3\n

where \n represents the LF character, signifying the end of a line. If you would add a fourth line here, it would indeed be just one new line.

Windows does not share this definition, and many Windows-based editors consider the CR+LF combination (noted below as \r\n) as line separator, not line terminator. As git noted with "\ No newline at end of file", your file was

1\r\n
2\r\n
3

When you added another line, you got

1\r\n
2\r\n
3\r\n
4

So from git's UNIX-centric point of view, you did modify line #3, by appending \r\n to it.

Now to the misunderstanding about diff output format: by default, diff is giving you the "unified" format, with some context around the changes. The changes are just the lines that have + or - prefixed to them: the original line 3 has been removed, and new lines 3 and 4 are inserted. The notation @@ -1,3 +1,4 @@ says that the snippet below describes lines 1-3 of the old file, and lines 1-4 of the new file. Normally, unified diff output defaults to also give you two lines of context before and after each change, to aid humans in understanding exactly what is changed (-U2). If you don't want the context, you can switch it off with -U0.

Upvotes: 0

Richard
Richard

Reputation: 108975

at row 1 three lines have been removed

No, rows 1 and 2 have not been changed. Diff shows some context, a few lines before and after the change, to make it easier to understand.

The diff is showing line 3 removed and lines 3 and 4 added. Line 3 changed because it didn't have an end of line before but does after (hence diff showing " at row 1 three lines have been removed").

Upvotes: 1

tmaj
tmaj

Reputation: 34947

You've changed line 3 and added line 4.

The change in line 3 was the new line.

git diff --unified=0 may make more sense (this shows only changed lines).

diff --git a/a.txt b/a.txt index 5f5fbe7..b178657 100644 --- a/a.txt +++ b/a.txt @@ -3 +3,2 @@ -3 \ No newline at end of file +3 +4 \ No newline at end of file

It's best seen in color:

enter image description here

Upvotes: 0

Related Questions