Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`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.

like image 915
valk Avatar asked Jan 21 '15 21:01

valk


People also ask

How do you get rid of M in VS code?

Search for \r (which is ^M) and replace with nothing (unless you want a newline, then replace with \n). Since you do not want all ^M's replaced, do not press replace all, choose the ones you want to replace.

How do I fix git line endings?

fix-git-line-endings #Set LF as your line ending default. #Save your current files in Git, so that none of your work is lost. #Remove the index and force Git to rescan the working directory. #Rewrite the Git index to pick up all the new line endings.

What does M mean in git diff?

^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.

What is M in git checkout?

That's the output of git status ; git is showing you that after checking out master there are still uncommited changes to your working copy (one modified file and one deleted file). Check man git-status : M = modified A = added D = deleted R = renamed C = copied U = updated but unmerged.


3 Answers

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
like image 134
Blake Frederick Avatar answered Sep 16 '22 15:09

Blake Frederick


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
like image 23
dapc Avatar answered Sep 17 '22 15:09

dapc


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
like image 45
Abdollah Avatar answered Sep 17 '22 15:09

Abdollah