git diff shows ^M with color ui true

15,987

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
15,987
ezdazuzena
Author by

ezdazuzena

Updated on July 23, 2022

Comments

  • ezdazuzena
    ezdazuzena over 1 year

    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.

  • ezdazuzena
    ezdazuzena almost 11 years
    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.
  • Xavier T.
    Xavier T. almost 11 years
    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.
  • ezdazuzena
    ezdazuzena almost 11 years
    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!
  • Xavier T.
    Xavier T. almost 11 years
    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.
  • kr37
    kr37 over 3 years
    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.