Tuesday, January 17, 2017

Force LF line endings on Windows with GIT

The possible git settings for line endings are:

  • Checkout Windows-style, commit Unix-style
    Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF.
    Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF.
    Git will not perform any conversions when checking out or committing text files. 
    git config --global core.autocrlf true
  • Checkout as-is, commit Unix-style
    git config --global core.autocrlf input
  • Checkout as-is, commit as-is
    git config --global core.autocrlf false

Solution on Stackoverflow:

The proper way to get LF endings in Windows is to first set core.autocrlf to false:
git config --global core.autocrlf false
Now git won’t do any line ending normalization.
If you want files you check in to be normalized, do this:
Set text=auto in your .gitattributes for all files:
* text=auto
And set core.eol to lf:
git config --global core.eol lf
Now you can also switch single repos to crlf (in the working directory!) by running
git config core.eol crlf
After you have done the configuration, you might want git to normalize all the files in the repo. To do this, go to to the root of your repo and run these commands:
git rm --cached -rf .
git diff --cached --name-only -z | xargs -n 50 -0 git add -f
If you now wano also normalize the files in your working directory, run these commands:
git ls-files -z | xargs -0 rm
git checkout .

Personal opinion: 
I set core.autocrlf to input, to make sure that the repo gets LF line endings always.

No comments:

Post a Comment