Enforce core.autocrlf=input through .gitattributes

2019-04-09 09:34发布

问题:

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

回答1:

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

Git doesn't convert on commit, but rather on git add. (More precisely, it does conversions on operations that copy the object into the repository and produce a hash value—but for most purposes, that's just git add anyway.) At this time it applies any "clean" filters and does input-side EOL operations. (Likewise, output-side "smudge" filters and EOL operations occur when copying out from the repository to the work-tree, which for most purposes is git checkout and git reset --hard.)

According to the gitattributes documentation, setting eol=lf:

... forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.

Hence, though I have not actually tested this, it sounds like * eol=lf is just what you want. Note that this is different from core.eol, which behaves as you described in your question; this is only for a .gitattributes setting, which applies to the files that match the name-pattern.