How to make Git ignore different line endings

warning: CRLF will be replaced by LF in [filename].
The file will have its original line endings in your working directory.

Getting errors like these, and want to deliberately ignore them?

If you don’t know why you would, you should read this great blogpost about line endings in Git by Tim Clem.

Don’t look at line endings!

In some cases you want to ignore these errors. In my specific situation i don’t want Git to warn me about the line endings, because my IDE takes care of that. I work in a project with multiple people, and somewhere in the process some files with different line endings than the project standard slipped into the repo. It happens. Your Git could be configured so that it automatically corrects the file line endings for you when you pull this code.  And this can be pretty annoying.

You can change this behaviour, but again, to avoid problems you have to know the reason why you get these errors. Be sure you know, see the link earlier in this post.

You can disable the CRLF behaviour completely, or per filetype by changing entries in your .gitattributes file. In my case, i put this:

* -crlf

This tells git to ignore the line endings for all files. And does not change the files in your working directory. Even if you have the core.autocrlf set to true, false, or input.

  • brettalton

    Does this work during a –diff however? It doesn’t appear to ignore the line endings if the changes have already been committed. I would be interested in seeing how you can compare two branches while ignoring the line ending.

  • Herf

    Couldn’t you use “* -crlf -diff” to handle the diff case?

    • stairbob

      No, that just makes git say “binary files […] differ” without showing any diff.

      • pinux

        why does ur repo even have binary file ? o_O

        • stairbob

          All files are binary, if you think about it.

  • Jen Wilson

    I don’t understand why you Git fanbois can’t tell the truth. It is garbage, and it does not handle line endings correctly. Your claim above is a boldface lie. Even if you add what you claim to .gitattributes, Git will still get confused by line ending problems and lie about what has changed. Linus hates users and he hates DOS users more. Because I use that Windows garbage, he wants me to die, and his over the top attacks on us to try to keep us from working shows just how horrible his kind is.

    • woolie

      @Jen Wilson: you are just a troll posting hate lines. Your post should be removed.
      I worked with Perforce, SVN and TFS before Git, and I can attest to the fact that Git is the best option out there, now even the teams inside Microsoft are using it.

    • pinux

      you just want everything out of box… thats why ur MS daddy is spanking ur system too hard with automatic-UPDATES. And yeah his kind are horrible.. you can just die now.

    • Jack Miller II

      I understand your apprehension with git, but if you are working with people using it, you can get on board or keep flaying your arms. This is not a Unix/Linux/AIX/Windows issue but a situation that is much more easily corrected in specific cases by simple changes in the .gitattributes files.
      We use:

      $ cat .gitattributes

      Set the default behavior, in case people don’t have core.autocrlf set.

      text=auto

      Declare files that will always have CRLF line endings on checkout.

      *.bat text eol=crlf

      Declare files that will always have LF line endings on checkout.

      *.sh text eol=lf

      But there are other answers you may prefer. As a Windoze user who also uses Linux/Unix/AIX (all processors) and others, I can complain about the differences or simply identify them and correct the problem. Much easier if you use the second option, but your call. “If it was easy, anyone could do it.” https://i.pinimg.com/originals/89/a6/de/89a6dee003cce85ae6dd66062c5e4600.jpg

      If that were true, this might be a much less profitable group of professions.

      • Russell R.

        If these solutions seemed to work, I’d agree, but they don’t.

  • Markus Peröbner

    I think the link to .gitattributes changed to http://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes ?

  • Lawrence

    You deserve a medal. Thank you very much!

  • Great stuff, thanks.

  • stairbob

    Am I misunderstanding something?


    $ cat .gitattributes
    * -crlf
    $ git diff | grep -C1 newline
    -}
    No newline at end of file

    +}

    -?>
    No newline at end of file
    +?>

  • Russell R.

    Github is still having this problem after 6 years? wow.