Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff thinks the last line of the file has been moved after the end of file?

After cloning a git repository from Github, if I open a file, make no changes, and save the file, the following shows up in the output of git diff:

-@import "sync.scss";
\ No newline at end of file
+@import "sync.scss";

As I understand it, \ No newline at end of file is supposed to mark the end of the file when no newline is present. Does this diff mean git thinks the last line has been moved to after the end of the file? Is there any way to avoid this? I'd like to contribute to this project without adding junk whitespace changes to my commits.

This seems like an issue with line endings. I'm fairly confident the file was originally saved on a Mac using Unix line endings. That's the same setup that I'm using, so I'm not sure what is causing the document to change when I save it.

like image 338
Michael Martin-Smucker Avatar asked Jul 10 '12 13:07

Michael Martin-Smucker


People also ask

Why git diff shows whole file changed?

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 is no new line at the end of file?

It indicates that you do not have a newline (usually \n , aka LF or CRLF) at the end of file. That is, simply speaking, the last byte (or bytes if you're on Windows) in the file is not a newline.

What is the difference between the git diff and git status?

The main difference between the commands is that git diff is specially aimed at comparisons, and it's very powerful at that: It can compare commits, branches, a single file across revisions or branches, etc. On the other hand, git status is specifically for the status of the working tree.


1 Answers

  • To avoid this, always do git add -p, and pick only relevant diffs to avoid unwanted changes to be committed.

  • And change your editor settings to match the conventions used in the project.

like image 156
Sailesh Avatar answered Sep 27 '22 22:09

Sailesh