Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git thinks I am rewriting one of my files everytime I make a small change

Tags:

git

diff

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?

like image 647
Paul Wicks Avatar asked Oct 28 '08 20:10

Paul Wicks


People also ask

Why does git show that all my files changed when I didn't change them?

These changes mean that metadata about your file changed, however the content of your file did not. If you're working in a group, this may start to intefere with pushes or just add noise to your commits.

What does git rewrite mean?

Git uses heuristics to determine if a change was a renaming or copying of a file, and also if it is a "rewriting" of the file. Roughly speaking, if the diff between the old and new version is bigger than the new version itself, it's a "rewrite".

What is git diff command?

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.


3 Answers

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.

like image 145
ddaa Avatar answered Sep 25 '22 02:09

ddaa


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.

like image 24
Paul Wicks Avatar answered Sep 23 '22 02:09

Paul Wicks


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.

like image 36
David Schmitt Avatar answered Sep 23 '22 02:09

David Schmitt