Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Why do my commits look like every line of the source was deleted and added?

+7 votes
Specifically, on GitHub, the "diff" that shows what changed makes it look like everything in the file was changed.  What's up?  Why did every line change, when I only changed a few lines of code?  

This makes it really hard to see what changed!
asked Sep 5, 2017 in CSC 285 (F17) by Forrest Stonedahl (100 points)

1 Answer

+3 votes
Best answer

For some reason, your system is adding both CR and NL (or LF) to the files (Windows-style line endings).   

Clarification CR means carriage return "\r", and NL (or LF) means new line (or line feed) "\n". Some Windows text files use two characters (\r\n) for every new line, whereas Unix and Mac use just one (\n). In general, we want git to use the single character version (Unix-style) when committing... there is a setting for git that does this, and it should be the default for git bash when you installed git, but it seems that Eclipse does not always recognize this.  The lab machines should all be configured correctly already, but if you're using your own laptop, then you need to do a couple steps to fix this (see below.)

Important: we need to get this straightened out before you and your team have a line-ending war, where person A changes every line of the file to end with "\r\n", and then person B commits which changes line back to "\n".  This will make it very hard for anyone on your team to see what's changing!

Solution: In Eclipse, go to Window -> Preferences -> Team -> git -> Configuration -> "add entry", and put:

key: core.autocrlf   (note: the last four letters are lowercase "CRLF")
value: true

That should fix it for the future!

If you have already committed a file with the "\r\n" line endings, and want to recommit it with the proper "\n" endings, then you should open that file in Eclipse again.  Changing a space somewhere or tweaking a comment is enough to let you re-save the file, and then add/commit/push it again.

If you or your team is having trouble with line endings (or inexplicably long diffs), let me know and I can help!

answered Sep 5, 2017 by Forrest Stonedahl (100 points)