我正在使用Linux git的Windows子系统(2.17.1版本)来管理我的Windows膝上型计算机上的存储库,但它让我对行尾设置感到头疼。
上下文
根据我对git中的EOL管理的了解(如果我错了,请纠正我的错误),有两个关键的方面可以管理这一点:
.gitconfig
,其中core.autocrlf
和core.eol
设置了通用策略.gitattributes
,在这里我可以指定每个路径的eol
和text
属性.从this question中,我了解到设置core.autocrlf = true
等同于设置core.eol = crlf
并将text=auto
属性添加到每个文件;eol=
属性覆盖core.eol
配置。
在此之后,我设置了一个小测试,以确定我需要哪些选项。
我想要得到的和我尝试过的
在我的存储库中,我将有一些文件仅在Windows端使用,有些文件仅在UNIX机器上使用,而源代码则将由两者使用。为了避免麻烦,我想把所有的文件都设置为总是有CRLF,同时在提交时将所有其他文件都转换为LF。
为了做到这一点,我想我应该在.gitattributes
中设置行*.vcxproj eol=crlf text
并完成它。为了更好的衡量,为了安全起见,我还想添加*.sh eol=lf text
。最后,我想出了一个小测试,看看它是否真的有效(剧透:它不起作用)。
测试设置
我的目录树如下所示:
root_dir/
- real.sh (has LF EOL)
- real.vcxproj (has CRLF EOL)
- fake.sh (has CRLF EOL)
- fake.vcxproj (has LF EOL)
real.*
文件的类型为“正确”的EOL,fake.*
有另一个。其目的是检查哪些文件在warning
git add
-ing到存储库时引发-ing。
git config -l
显示:
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
理想情况下,如果一切都像我所理解的那样工作,那么“假”文件就会引发正在转换为相反类型的警告。
相反,所发生的是:
$ git add -A
warning: CRLF will be replaced by LF in fake.sh.
The file will have its original line endings in your working directory.
warning: CRLF will be replaced by LF in real.vcxproj.
The file will have its original line endings in your working directory.
为了理解发生这种情况的原因,转换似乎选择了core.eol
配置(在UNIX系统上默认为LF ),并忽略了我在.gitattributes
中指定的内容。为了验证这一假设,我设置了core.eol = crlf
,重新设置索引,然后再次进行git add -A
,这次获得:
$ git add -A
warning: LF will be replaced by CRLF in fake.vcxproj.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in real.sh.
The file will have its original line endings in your working directory.
假设得到证实,头痛增加。为什么我的eol=...
属性被忽略了?是否有一种方法可以根据每个文件类型配置自动转换?如果是这样的话,是怎么做的?
发布于 2018-07-03 12:16:46
经过进一步的测试,一些不可见字符似乎最终出现在我的.gitattributes
文件中,从而破坏了eol=...
属性。通过cat "..." > .gitattributes
生成文件解决了问题。
https://stackoverflow.com/questions/51086499
复制相似问题