9

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

git-diff ouputs 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 doing a 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? Is this a problem? If it is, how do I fix this?

John Topley
  • 113,588
  • 46
  • 195
  • 237
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146

4 Answers4

27

Clearly, git does not like your mac-style line endings (CR only). Its diff algorithm uses LF as the line separator.

Fix your files to have windows-style (CR LF) or unix (LF only) line endings.

ddaa
  • 52,890
  • 7
  • 50
  • 59
  • 2
    OS X uses LF endings just like every other style of Unix out there. – Paul Wicks Oct 30 '08 at 19:13
  • 8
    So called "mac-style" line endings were standard on Macs up to MacOS 9. And only on MacOS. – ddaa Oct 30 '08 at 23:59
  • 7
    It's pretty sad this answer was downmodded four times, while the diagnostic was correct, as the question author noted in his answer. – ddaa Oct 31 '08 at 00:14
20

To fix this, I didn't need to change any of the core git settings, as the default line endings being generated were fine, it was just that this particular file was mangled. To fix it I opened vim and executed the following command

:%s/^M/\r/g

Note that to type the "^M" you have to type ctrl-V and then ctrl-M.

Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
4

Set core.autocrlf and core.safecrlf with git-config. This will cause git to automatically convert line endings when transferring from/to the object store. You might need to make a commit to store the "new" endings.

Judging from your pasted example, you might be also suffering from "old-style Mac line endings" (thanks to ddaa and Charles Bailey for the hint), which are only bare CRs without any LF, a case not handled by git. If this is true (check with a hex editor), use a tool like recode to translate this garbage into some 21st century format, like proper LF-only Unix line endings.

Community
  • 1
  • 1
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
  • 1
    core.autocrlf is not going to help in this case (old-style Mac line endings). To quote the current git source: "We're currently not going to even try to convert stuff that has bare CR characters. Does anybody do that crazy stuff?" – CB Bailey Oct 29 '08 at 09:13
0
git diff -b

Ignores end of line changes when showing you the differences.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338