git-diff to ignore ^M

In a project where some of the files contains ^M as newline separators. Diffing these files are apparently impossible, since gitdiff sees it as the entire file is just a single line.

How does one diff with the previous version?

  • Edit all changes in git?
  • Git diff creating empty patch when using external difftool
  • git diff - handling long lines?
  • Git search all diffs
  • git patches won't apply
  • Why git diff shows identical files as modified?
  • Is there an option like “treat ^M as newline when diffing” ?

    prompt> git-diff "HEAD^" -- 
    diff --git a/myproject/ b/myproject/
    index be78321..a393ba3 100644
    --- a/myproject/MyFile.cpp
    +++ b/myproject/MyFile.cpp
    @@ -1 +1 @@
    -<U+FEFF>import;^Mimport mx.controls.*;^Mimport mx.utils.Delegate
    \ No newline at end of file
    +<U+FEFF>import;^Mimport mx.controls.*;^Mimport mx.utils.Delegate
    \ No newline at end of file


    now I have written a script that checks out the latest 10 revisions and converts CR to LF.

    require 'fileutils'
    if ARGV.size != 3
      puts "a git-path must be provided"
      puts "a filename must be provided"
      puts "a result-dir must be provided"
      puts "example:"
      puts "ruby gitcrdiff.rb project/dir1/dir2/dir3/ SomeFile.cpp tmp_somefile"
    gitpath = ARGV[0]
    filename = ARGV[1]
    resultdir = ARGV[2]
    unless FileTest.exist?(".git")
      puts "this command must be run in the same dir as where .git resides"
    if FileTest.exist?(resultdir)
      puts "the result dir must not exist"
    10.times do |i|
      revision = "^" * i
      cmd = "git show HEAD#{revision}:#{gitpath}#{filename} | tr '\\r' '\\n' > #{resultdir}/#{filename}_rev#{i}"
      puts cmd 
      system cmd

  • Github: Reject commits containing DOS newline characters
  • Remove an unwanted merge commit and rebase instead with Git
  • Date modified in file system does not change when switching branches in Git repo
  • Github pulling does not work in - Rails 5
  • Is there a way to shorten branch names in git commands?
  • Android Studio - .iml file changes its name
  • 7 Solutions collect form web for “git-diff to ignore ^M”

    GitHub suggests that you should make sure to only use \n as a newline character in git-handled repos. There’s an option to auto-convert:

    $ git config --global core.autocrlf true

    Of course, this is said to convert crlf to lf, while you want to convert cr to lf. I hope this still works …

    And then convert your files:

    # Remove everything from the index
    $ git rm --cached -r .
    # Re-add all the deleted files to the index
    # You should get lots of messages like: "warning: CRLF will be replaced by LF in <file>."
    $ git diff --cached --name-only -z | xargs -0 git add
    # Commit
    $ git commit -m "Fix CRLF"

    core.autocrlf is described on the man page.

    Developing on Windows, I ran into this problem when using git tfs. I solved it this way:

    git config --global core.whitespace cr-at-eol

    This basically tells Git that an end-of-line CR is not an error. As a result, those annoying ^M characters no longer appear at the end of lines in git diff, git show, etc.

    It appears to leave other settings as-is; for instance, extra spaces at the end of a line still show as errors (highlighted in red) in the diff.

    (Other answers have alluded to this, but the above is exactly how to set the setting. To set the setting for only one project, omit the --global.)


    After many line-ending travails, I’ve had the best luck, when working on a .NET team, with these settings:

    • NO core.eol setting
    • NO core.whitespace setting
    • NO core.autocrlf setting
    • When running the Git installer for Windows, you’ll get these three options:
      • Checkout Windows-style, commit Unix-style line endings <– choose this one
      • Checkout as-is, commit Unix-style line endings
      • Checkout as-is, commit as-is

    If you need to use the whitespace setting, you should probably enable it only on a per-project basis if you need to interact with TFS. Just omit the --global:

    git config core.whitespace cr-at-eol

    If you need to remove some core.* settings, the easiest way is to run this command:

    git config --global -e

    This opens your global .gitconfig file in a text editor, and you can easily delete the lines you want to remove. (Or you can put ‘#’ in front of them to comment them out.)

    Also see:

    core.whitespace = cr-at-eol

    or equivalently,

        whitespace = cr-at-eol

    where whitespace is preceded by a tab character.

    Try git diff --ignore-space-at-eol, or git diff --ignore-space-change, or git diff --ignore-all-space.

    Why do you get these ^M in your git diff?

    In my case I was working on a project which was developed in Windows and I used OS X. When I changed some code, I saw ^M at the end of the lines I added in git diff. I think the ^M were showing up because they were different line endings than the rest of the file. Because the rest of the file was developed in Windows it used CR line endings, and in OS X it uses LF line endings.

    Apparently, the Windows developer didn’t use the option “Checkout Windows-style, commit Unix-style line endings” during the installation of Git.

    So what should we do about this?

    You can have the Windows users reinstall git and use the “Checkout Windows-style, commit Unix-style line endings” option. This is what I would prefer, because I see Windows as an exception in it’s line ending characters and Windows fixes it’s own issue this way.

    If you go for this option, you should however fix the current files (because they’re still using the CR line endings). I did this by following these steps:

    1. Remove all files from the repository, but not from your filesystem.

      git rm --cached -r .
    2. Add a .gitattributes file that enforces certain files to use a LF as line endings. Put this in the file:

      *.ext text eol=crlf

      Replace .ext with the file extensions you want to match.

    3. Add all the files again.

      git add .

      This will show messages like this:

      warning: CRLF will be replaced by LF in <filename>.
      The file will have its original line endings in your working directory.
    4. You could remove the .gitattributes file unless you have stubborn Windows users that don’t want to use the “Checkout Windows-style, commit Unix-style line endings” option.

    5. Commit and push it all.

    6. Remove and checkout the applicable files on all the systems where they’re used. On the Windows systems, make sure they now use the “Checkout Windows-style, commit Unix-style line endings” option. You should also do this on the system where you executed these tasks because when you added the files git said:

      The file will have its original line endings in your working directory.

      You can do something like this to remove the files:

      git ls | grep ".ext$" | xargs rm -f

      And then this to get them back with the correct line endings:

      git ls | grep ".ext$" | xargs git checkout

      Of course replacing .ext with the extension you want.

    Now your project only uses LF characters for the line endings, and the nasty CR characters won’t ever come back :).

    The other option is to enforce windows style line endings. You can also use the .gitattributes file for this.

    More info:

    I struggled with this problem for a long time. By far the easiest solution is to not worry about the ^M characters and just use a visual diff tool that can handle them.

    Instead of typing:

    git diff <commitHash> <filename>


    git difftool <commitHash> <filename>

    If you are using Eclipse, you can make the ^M disappear from git diff by setting File > Convert Line Delimiter To > Unix (LF, \n, 0A, ¶)

    Git Baby is a git and github fan, let's start git clone.