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.

  • Cloud9 IDE git setup
  • What is the Working Directory of a GIT repository?
  • What to do if I forgot to do a git svn rebase before merge?
  • Tag messages on github
  • Using 'git pull' vs 'git checkout -f' for website deployment
  • git merge: filter files to avoid silly conflicts (like whitespace or case changes)
  • Can I force LF line endings without requiring specific git config settings, and without losing the automatic text/binary inference?

  • How to delete all merged code changes from a specific git branch
  • How can I add pattern that start with hash to gitignore
  • Eclipse/Git - Pull Failed Dirty Worktree
  • How do I make vim my editor of choice for git notes?
  • add blobs or trees to a created repository using pygit2 (libgit2)?
  • How can I tell git (or some other dvcs) to track a file privately?
  • 2 Solutions collect form web for “Force LF line endings using .gitattributes without losing automatic text/binary inference?”

    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.

    I suggest three solutions:

    METHOD 1
    I assume most of your working directory files are text files. Hence it waste a lot of time to add specific line for each text file type.

    You can unset text attribute for binary files in .gitattributes file. Since only a few kind of binary file type, it is not tedious to do so.

    METHOD 2
    I think your key concern is disabling the global eol=lf settings. According to the .gitattributes precedence, you can config the text=auto in your $GIT_DIR/info/attributes which has the highest priority. Meanwhile, it only affects the working tree.

    METHOD 3
    Totally remove the .gitattributes from your working tree. Depend on your file editor for end-of-line conversion. I recommend Notepad++ which is handy for EOL conversion between Unix-style and Windows-style.

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