9

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.

3 Answers 3

15

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.

4
  • What exactly do you mean with "git diff window"? I just do this in the shell and I don't get any specific window of a vim kind.
    – ezdazuzena
    Commented Jan 22, 2013 at 16:49
  • 4
    Ah sorry, I thought you were using "vim diff" as the diff tool for git. So if you open the file with Vim, can you give the response from :set fileformat?. See vim.wikia.com/wiki/File_format for more information.
    – Xavier T.
    Commented Jan 22, 2013 at 16:58
  • 1
    fileformat=dos. When switching it to unix the ^M disappears. Great! I'm having some side effects that somehow all my code appears as changed, but basically this is the solution to my problem. Thanks!
    – ezdazuzena
    Commented Jan 22, 2013 at 17:05
  • 1
    Probably one of your editor, or the action to check-in converted the end line of your file. Try to find the root cause, because you don't want to spend your time converting file format. Check if the git autocrlf setting is not set.
    – Xavier T.
    Commented Jan 22, 2013 at 17:16
7

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.

1
  • 1
    Following your post, this worked for me: git config --global core.whitespace cr-at-eol. I suppose one might first do git config --global core.whitespace to see what it is set to.
    – kr37
    Commented Mar 9, 2020 at 13:08
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.