Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: editing a file cause ^M to appear when using git diff

Tags:

git

linux

vim

gedit

I have a couple of source files inside a local git repo. The files have been pulled from a remote git repo. When I do a "$git status" I see the following:

$git status

# On branch master
nothing to commit, working directory clean

However, when I edit any source file (using vim or gedit), ^M always gets added to the end of lines that I add. In other words, after I make the changes I see the following:

$git status

# On branch master
# Changes not staged for commit: 
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   sourcefile1.c
#
no changes added to commit (use "git add" and/or "git commit -a")

$git diff sourcefile1.c
...
+ This is a test line ^M
...

I do not know why this is happening but it seems that it is not related to the editor I use nor git itself. I am aware that there is a way to force git to ignore ^M, but I really want to know what is going wrong, why these ^M are being inserted, and how I can stop that from happening.

I am using Ubuntu with Linux kernel 3.11.0

I appreciate any help.

like image 243
MaACAn Avatar asked Mar 19 '23 21:03

MaACAn


2 Answers

When you do a git-diff it will warn if there is a CR (the ^M) at the end of a line. This is considered a "whitespace error" and will show up in RED by default irrespective of whether there is a real diff. You can get it to shut up by creating a .gitattributes file containing the line

* whitespace=cr-at-eol

This won't affect the file contents, it just disables the warning from git-diff.

You don't say whether the file originally had CRLF line endings, but it is highly unlikely that Vim converted it from Unix to DOS line endings without warning you. I find line ending conversions in git to be a real pain and confusing and just switch them off in .gitattributes by adding a line

* -text

This stops git doing any magic conversions and leaves it up to you.

You probably want to check all the files in your working directory to see what line endings they currently have (Vim should tell you in the status bar). See also Git line endings after normalization for a couple of git aliases that can convert from one to the other. If this is a Linux-only repo doing a one-off conversion of everything to Unix format might be a good idea.

like image 111
Philip Daniels Avatar answered Mar 22 '23 10:03

Philip Daniels


I have faced this problem once and I was able to solve it with the "dos2unix" program. I think FDinoff is right in that vim uses the dos line endings because the file originally has the dos format. To change it back to a unix format, install dos2unix (if you don't already have it). For Ubuntu, you can do this by:

sudo apt-get install dos2unix

Then use this command to convert your file format, e.g.

dos2unix sourcefile1.c

After this, if the file format was actually the problem, you should not see the ^M when you add new lines anymore.

like image 40
StRiDeR Avatar answered Mar 22 '23 09:03

StRiDeR