Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff shows ^M with color ui true

Tags:

git

vim

I modified my .gitconfig in such a way that I have some colors when performing git diff:

$ cat .gitconfig 
[color]
ui = true

I'm working on an ubuntu machine and I edited some code using VIM. After editing a file a execute git diff, once with and once without ui=true.

Problem: in the first case I have ^M characters and the end of the edited lines. However, I don't see those when turning of color.ui or when looking with vim, cat, more.. at the manipulated file.

like image 457
ezdazuzena Avatar asked Jan 22 '13 15:01

ezdazuzena


3 Answers

It is probably an encoding issue. The 'git diff' command is executing Vim thinking that the file format is Dos.

When you are using any other command, it is correctly recognized as a Unix file.

Can you try : :set fileformat=unix in your git diff window ?

I am not really sure this is the root cause, because I don't see the link with the ui option.

like image 170
Xavier T. Avatar answered Nov 03 '22 22:11

Xavier T.


The problem was solved for me by setting core.whitespace to cr-at-eol

treats a carriage-return at the end of line as part of the line terminator (git-config docs)

This can be added to a per-project or global git config file as follows:

[core]
    whitespace = cr-at-eol

This solves the problem of hiding the ^M at the end of lines which were in the diff due to another non-line-ending change. The intention is not to ignore changes where the only difference is the line ending. I am on windows with core.autocrlf=true and so the line ending mismatch is expected.

My only caution is that I don't know if this will affect whether git flags up genuine changes in EOL which one might want to commit, but perhaps with autocrlf=true this is never going to be the case.

An alternative fix, more targeted (but slightly more hacky), is described here.

like image 39
sparrowt Avatar answered Nov 03 '22 22:11

sparrowt


Please have a look at Github's excellent "Dealing with line endings" page:

https://help.github.com/articles/dealing-with-line-endings

You probably want to set core.autocrlf and then re-normalize your repository.

like image 38
user1338062 Avatar answered Nov 03 '22 23:11

user1338062