Git thinks that I rewrite one of my files every time I make a small change

I have a medium sized Java file. Every time I make changes to one of my files, BuildTable.java, Git reports this as a massive change, even if it is only a line or two. BuildTable.java is about 200 lines, and a change in this commit changed only one line.

git-diff does this:

--- a/src/BuildTable.java +++ b/src/BuildTable.java @@ -1 +1 @@ -import java.io.FileNotFoundException;^Mimport java.io.FileReader;^Mimport java.io.InputStreamReader;^Mimport java.io.PushbackReader;^Mimport java.util.ArrayList;^Mimport \ No newline at end of file +import java.io.FileNotFoundException;^Mimport java.io.FileReader;^Mimport java.io.InputStreamReader;^Mimport java.io.PushbackReader;^Mimport java.util.ArrayList;^Mimport \ No newline at end of file 

After running git -commit -a

 Created commit fe43985: better error notifications 3 files changed, 54 insertions(+), 50 deletions(-) rewrite src/BuildTable.java (78%) 

Is git seeing this file as binary or something else? This is problem? If so, how to fix it?

+9
git diff
Oct 28 '08 at 20:01
source share
4 answers

To fix this, I did not need to change any basic git settings, since the resulting final lines were fine by default, just this file was corrupted. To fix this, I opened vim and ran the following command

 :%s/^M/\r/g 

Note that to enter "^ M" you need to enter ctrl-V and then ctrl-M.

+20
Oct 30 '08 at 19:17
source share

Clearly, git doesn't like your Mac-style line endings (CR only). Its diff algorithm uses LF as a line separator.

Correct your files to have window style line endings (CR LF) or unix (LF only).

+27
Oct 28 '08 at 20:24
source share

Install core.autocrlf and core.safecrlf using git-config . This will cause git to automatically convert line endings when passing from / to the object store. You may need to make a commit to store the "new" endings.

Judging by your plugin example, you also suffer from “old-style Mac line endings” (thanks to ddaa and Charles Bailey for a hint), which are just bare CR without any LF , a case that is not handled by git. If this is true (check with a hex editor), use a tool such as recode to convert this garbage to 21st century format, such as the correct LF - only the end of Unix lines.

+5
Oct 28 '08 at 20:40
source share
 git diff -b 

Ignores the changes at the end of the line, showing you the differences.

0
Nov 29 '09 at 7:28
source share



All Articles