Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force git to use LF instead of CR+LF under windows?

Tags:

git

msysgit

I want to force git to checkout files under Windows using just LF not CR+LF. I checked the two configuration options but I was not able to find the right combination of settings.

I want it to convert all files to LF and keep the LF on the files.

Remark: I used autocrlf = input but this just repairs the files when you commit them. I want to force it to get them using LF.

Probably I wasn't so clear: the repository is already using LF but the files checked out using msysgit are using CR+LF and I want to force msysgit to get them with LF: forcing Unix line endings.

>git config --list | grep crlf core.autocrlf=input 
like image 831
sorin Avatar asked Mar 25 '10 16:03

sorin


People also ask

How do I change from CRLF to LF in git?

text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. text eol=lf Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.

Can I use LF in Windows?

If your system/IDE/tooling support LF and you do want to use LF as everyone else in your team without any silent lf->crlf->lf normalizations, you must turn off autocrlf and configure eol to not infer native line endings, but force it to use lf .

Should I use CRLF or LF?

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. The history of these two control characters dates back to the era of the typewriter.


2 Answers

The proper way to get LF endings in Windows is to first set core.autocrlf to false:

git config --global core.autocrlf false 

You need to do this if you are using msysgit, because it sets it to true in its system settings.

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 want git to also normalize the files in your working directory, run these commands:

git ls-files -z | xargs -0 rm git checkout . 
like image 112
Chronial Avatar answered Sep 21 '22 17:09

Chronial


I come back to this answer fairly often, though none of these are quite right for me. That said, the right answer for me is a mixture of the others.

What I find works is the following:

 git config --global core.eol lf  git config --global core.autocrlf input 

For repos that were checked out after those global settings were set, everything will be checked out as whatever it is in the repo – hopefully LF (\n). Any CRLF will be converted to just LF on checkin.

With an existing repo that you have already checked out – that has the correct line endings in the repo but not your working copy – you can run the following commands to fix it:

git rm -rf --cached . git reset --hard HEAD 

This will delete (rm) recursively (r) without prompt (-f), all files except those that you have edited (--cached), from the current directory (.). The reset then returns all of those files to a state where they have their true line endings (matching what's in the repo).

If you need to fix the line endings of files in a repo, I recommend grabbing an editor that will let you do that in bulk like IntelliJ or Sublime Text, but I'm sure any good one will likely support this.

like image 43
Ben Liyanage Avatar answered Sep 21 '22 17:09

Ben Liyanage