gitnewline

Force LF line endings using .gitattributes without losing automatic text/binary inference?


I would like to use eol=LF in my .gitattributes file, but I would like it to apply only to the files Git automatically determines to be text files.

The best I could find is to define specific file extensions / globs as text or binary. This is not ideal as the list could be huge. I've tried * text=auto eol=LF, but the eol=LF part appears to override the auto part.

Can I force LF line endings without requiring specific git config settings, and without losing the automatic text/binary inference?


Solution

  • Update: after @romkyns comment I rechecked everything and found my solution to be slightly incorrect. Correct would be a .gitattributes-file with the following content:

    * text=auto
    

    According to the documentation, this ensures that all files that Git considers to be text will have normalized (LF) line endings in the repository.


    Original Answer:

    What you want to write in your .gitattributes is as simple as this:

    * text=auto
    * text eol=lf
    

    First line tells git to autodetect file types (binary or text, which is the default behavior of git anyway and therefore may be omitted), second line treats the line endings of all detected text files (and only those) with a LF end of line.

    I tested this setting with some mixed binary contents and some text files with CRLF ending and got the expected conversion to LF ending.