valk
valk

Reputation: 9894

`git add` adds ^M to the end of every line

I'm on Ubuntu 14.04. I'm editing files with Vim. Suddenly I started to notice that the changes that I make which I see with git diff filename contain ^M at the end of every line that I've inserted or changed. So after I run git add to the filename I see with git diff --staged that every line has ^M at the end and thus it's like if I made a change to the whole file even if I changed only one line. Please help me to understand what's going on here.

Upvotes: 23

Views: 16532

Answers (3)

Blake Frederick
Blake Frederick

Reputation: 1670

Are your files being checked in from a Windows computer at any point? Windows adds CR+LF to line endings, while other OS's use LF only. If you've set core.autocrlf to false then git diff will highlight CR characters as ^M. To turn this off, you can alter the core.whitespace setting:

git config --global core.whitespace cr-at-eol

Upvotes: 22

Abdollah
Abdollah

Reputation: 5177

I faced a similar problem when I copied my whole project directly from Windows to Linux. Regarding this document, I ran the following command on my Linux Terminal and the problem was resolved:

$ git config --global core.autocrlf input

Upvotes: 6

dapc
dapc

Reputation: 334

This solved this problem for me, I quote from following source: core.autocrlf explained

Hope this helps someone!

core.autocrlf

If you’re programming on Windows and working with people who are not (or vice-versa), you’ll probably run into line-ending issues at some point. This is because Windows uses both a carriage-return character and a linefeed character for newlines in its files, whereas Mac and Linux systems use only the linefeed character. This is a subtle but incredibly annoying fact of cross-platform work; many editors on Windows silently replace existing LF-style line endings with CRLF, or insert both line-ending characters when the user hits the enter key.

Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code:

$ git config --global core.autocrlf true

Upvotes: 11

Related Questions