Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce core.autocrlf=input through .gitattributes

Is there a way to enforce core.autocrlf=input from within the .gitattributes in order to propagate the policy throughout my colleagues?

In detail what I want is to convert to lf on add and leave as is on checkout.

The problem is that neither text nor eol do what I want in .gitattributes since eol has 3 acceptable values:

  1. lf
  2. crlf
  3. native

Ideally I would like my .gitattributes file to look like this:

* text eol=asis

like image 211
laertis Avatar asked Mar 08 '17 09:03

laertis


People also ask

What is core Autocrlf input?

autocrlf = input This means that Git will process all text files and make sure that CRLF is replaced with LF when writing that file to the object database. It will not, however, do the reverse.

Should I use LF or CRLF?

Whereas Windows follows the original convention of a carriage return plus a line feed ( CRLF ) for line endings, operating systems like Linux and Mac use only the line feed ( LF ) character.

What is Git config global core Autocrlf input?

The git config core. autocrlf command is used to change how Git handles line endings. It takes a single argument. On Windows, you simply pass true to the configuration.

What should Autocrlf be set to?

Theoretically it is said that when working in a windows machine, people should use core. autocrlf set to true.


1 Answers

It isn't clear exactly what you mean by "as is" in the working tree. If you want Git to store line endings in LF in the repository and let the user decide what they want in the working tree (based on their platform and/or settings), then use this:

* text

That will force line ending conversion on, and Git will convert files to LF in the repository and will honor the user's preferred line endings on checkout. If not all of your files are text (i.e., you have images or other binaries) and you'd like Git to guess, then you can use this:

* text=auto

Note that as mentioned by LoopInFool, this will not cause files that are already CRLF to be converted, so you'll need to be sure that your files are already in LF in the repository, or list those files types explicitly as text (e.g., *.c text).

The behavior of core.autocrlf=input is to force conversion to LF on addition into the repository and not to perform any conversion on checkout; that is, to always use LF endings regardless of the user's settings. If that's the behavior you want, then you can do that with the following:

* eol=lf

Note that setting eol effectively sets the text attribute, so you should not set it on any files that are binary.

If what you want is for Git to match the line endings that are already in the working tree on checkout (e.g, by reading the file), then it won't do that. Git always does line ending conversions based on configuration without regard to what's already there, so the user will have to indicate their preference in some way or accept the platform default behavior.

In addition, Git always counts any modification to the file size as a modification in git status, even if it ignores those changes on add (say, because you've only changed line endings); this is similarly not avoidable.

If what you want is something entirely different, please go into a little more detail about what behavior you want, and I'll update with more details.

like image 69
bk2204 Avatar answered Sep 21 '22 08:09

bk2204