20

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?

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324

3 Answers3

8

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.

Lars
  • 5,757
  • 4
  • 25
  • 55
  • 1
    Why doesn't the second line make everything `text`? I am surprised that it doesn't, [the documentation](http://git-scm.com/docs/gitattributes) makes it look like the `* text` part _"marks the path as a text file"_. Also, I'd like this to override any git config settings, will that happen even if I omit the first line like you suggest? – Roman Starkov Nov 24 '15 at 12:20
  • You are completely right - Found a solution after all. First line is enough to do what you ask for. As for your other question, the repository settings override the user settings, which override the system settings: see https://git-scm.com/book/tr/v2/Customizing-Git-Git-Configuration#_git_config – Lars Nov 24 '15 at 20:48
  • 1
    I think we'll go with `* text=auto`. I do wonder how one might specify text=auto while also specifying the eol option where necessary, but I realise that I didn't ask that in my original question. – Roman Starkov Nov 29 '15 at 15:22
  • 4
    I do not think this is full answer. `* text=auto` still converts on Linux file endings to CRLF on Windows. The question is how to force `eol=LF` which means that also on Windows files should be checked out with LF file endings. – Mitar Sep 28 '18 at 05:44
  • 1
    I believe `* text=auto` is already the default when not using a `.gitattributes` file. This solution doesn't work on Windows when `autocrlf` is set unfavorably. – jlh Feb 23 '21 at 09:15
3

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.

Lars
  • 5,757
  • 4
  • 25
  • 55
Zachary
  • 1,633
  • 2
  • 22
  • 34
0

Repositery level git attributes : ( Where should I place my global 'gitattributes' file?)

-create a file .git/info/attributes with in it: * text=auto

-For each modified files, when doing git add *, you should now be warned “the file will have its original line endings in your working directory”

Matoeil
  • 6,851
  • 11
  • 54
  • 77