What will `* text=auto eol=lf` in gitattributes do?
Asked Answered
A

3

49

We have this in our .gitattributes file:

* text=auto eol=lf

I'd like to precisely understand what this does.

The first part is text=auto. From the documentation:

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

The important part is that Git does the normalization only for files that it detects as text files.

However, I'm not sure about the eol=lf part. I would think that it will also do the normalization only for text files but I can't find support for it in the documentation and we had an instance when our PNG files were normalized too, making them invalid.

Is there a settings like the above that would basically say "do the normalization in both directions for text files, and leave binary files alone"?

Annia answered 3/4, 2015 at 15:32 Comment(0)
A
44

Git 2.10 fixed this and now behaves as one would expect.

Annia answered 5/12, 2016 at 14:52 Comment(1)
My Git Client was in 2.9.15, updating to le last one 2.15 solve my problem. Thank youDarfur
A
20

The answer is no, Git currently (as of 2.3) cannot do checkout EOL conversion with auto-detection of binary and text formats so that it processes text only. The workaround is either to specify eol=lf only for selected file types (e.g., *.txt) or, inversely, mark certain file types as binary using e.g. *.png binary).

Related: feature proposal on Git mailing list


* text=auto

This will correctly normalize text files in the repo. However, the second part (LF forcing on checkout) cannot be achieved easily today because adding eol=lf will unfortunately process binary files too. The only solution today is to mark certain types for conversion (e.g., *.txt eol=lf) or, inversely, mark certain types as binary (e.g., *.png binary).

Both of these suffer from the same issue: the specific file types must be listed explicitly in the .gitattributes file, which means that either the types must be known ahead of time or all developers must remember to update the .gitattributes file every time a new file type appears in the project. Which they won't.

Annia answered 8/4, 2015 at 7:41 Comment(1)
This is fixed in modern Git versions. The versions in Ubuntu 18.04 and 20.04 work correctly.Rivi
P
7

This answer is for others who may stumble here as I did.

As of Git 2.10 this works as intended, the below is only relevant to 2.10 (check version with git --version). Relevant snippet from Git 2.10 release notes

* text=auto eol=lf behaves as git config core.autocrlf false

* text=auto eol=crlf behaves as git config core.autocrlf true

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

This lf mentioned in this quote is referring to what happens when a file is added to the index (and finally pushed upstream). The additional eol=xx says what these files should be in your working tree, ie how they should appear locally on your filesystem after checking out files that git has automatically detected as text via the * text=auto portion.

Is there a settings like the above that would basically say "do the normalization in both directions for text files, and leave binary files alone"?

Either would work, but personally I use the below in the .gitattributes file. The order does matter.

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ahk text eol=crlf
Prussian answered 7/3, 2022 at 1:52 Comment(2)
"* text=auto eol=lf behaves as git config core.autocrlf false " I don't think that this statement is true. With autocrlf set to false, git actually doesn't care about line endings at all. You'll be able to push LF, CR as well as CRLF into your repository with no conversions. With autocrlf set to true, you'll have CRLF in your working directory, but only LF is ever going to get pushed into the repository itself. Please correct me if I'm wrong, I'm trying to get a grasp at this subject matter myself right now.Sedan
@Nefiji: With autocrlf set to false, the behavior is governed by the effective text attribute of each file and the eol setting. With autocrlf set to true, Git behaves as if all files are set to text=auto and as if eol is set to "crlf". That is, it performes auto-detection of text files (by inspecting the contents of files) and then uses CRLF in the working directory for files detected as being text.Weatherboarding

© 2022 - 2024 — McMap. All rights reserved.