Git can't check out on Linux with LF a file th

2020-04-19 07:02发布

问题:

I am checking out a third party project "libjpeg" from https://github.com/winlibs/libjpeg on Linux (it is just one example, actually I have the same problem with many other projects as well). I have the following Git line endings configuration.

I have only global settings configured (set to checkout with LF line endings):

$ git config --system -l | grep core
core.eol=lf
core.autocrlf=false

$ git config --global -l | grep core
core.eol=lf
core.autocrlf=false

There are no local (repo) settings regarding line endings.

I read this article about the Git line endings configuration: https://adaptivepatchwork.com/2012/03/01/mind-the-end-of-your-line and I think that my configuration should give me LF for all text files on Linux. However it does not work. I get CRLF instead in my workspace. For example:

~/gitclonetest/libjpeg$ file libjpeg.map
libjpeg.map: ASCII text, with CRLF line terminators
~/gitclonetest/libjpeg$ cat -v libjpeg.map
LIBJPEG_9.0 {^M
  global:^M
    *;^M
};^M

Can someone help me to understand what I am missing? My goal is to have all text files (for any project) automatically be converted to LF when I clone it on Linux host.

UPDATE: The goal is to configure Git to check out on Linux host with LF even if then file was stored with CRLF in the repository.

回答1:

The problem is that you have core.autocrlf set to true. The documentation says the following:

Setting this variable to "true" is the same as setting the text attribute to "auto" on all files and core.eol to "crlf". Set to true if you want to have CRLF line endings in your working directory and the repository has LF line endings.

You definitely don't want to set that variable to true on a Unix or Linux system; it should be set to false unless you're on a Windows system (and even then there are better alternatives).



回答2:

You more than likely have set up some of the flags that tell git to mess up with EOL formats (and those flags are a mess). If you would rather have git not mess with them, you can do so by adding this to .gitattributes:

* -text

That way git won mess with the files when you add them or checkout. If you need some other kind of thing (like, real automatic EOL conversion), you might check the available things there.

https://git-scm.com/docs/gitattributes

Either way, steer away from using the flags you used on the question. They are a mess.



回答3:

Old, but still correct answer about EOL-headache in Git

In short:

core.autocrlf = false 
core.eol = native

will produce correct EOLs on all and any mix of OSes