在Windows中覆盖.gitattributes的text=auto

8

这非常不直观:

C:\python-tdl\examples\termbox>git config core.autocrlf
false

C:\python-tdl\examples\termbox>git commit termbox.py
warning: LF will be replaced by CRLF in examples/termbox/termbox.py.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in examples/termbox/termbox.py.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in examples/termbox/termbox.py.
The file will have its original line endings in your working directory.
Aborting commit due to empty commit message.

根据各种媒体的说法,使用core.autocrlf=false时,不应进行任何换行符转换。

在项目根目录中,我发现了一个.gitattributes文件并包含以下行:

# Auto detect text files and perform LF normalization
* text=auto

如果我进行注释,警告就会消失。问题是 - 如何自动覆盖这个 .gitattributes 设置?

2个回答

7

.gitattributes 覆盖了所有的配置设置,因此它无法被覆盖;可以说它是“覆盖者”。虽然你可以简单地删除这一行,但如果其他开发者的机器上有 core.autocrlf=true,这将导致不一致的行为。所以最好的方法是在 .gitattributes 中添加以下行:* -text。这将禁用所有文件的 CRLF 处理。


3
如果GIT有一些选项可以完全关闭EOL转换,而不考虑.gitattributes文件,那就太好了。在我的情况下,.gitattributes文件是由RE维护的,我无法更改它,但我绝对希望将PC上的所有文件都只用LF。 - Andrei LED
非常感谢。我花了很长时间与一个设置了 * text=auto 的快速导出进行斗争。这个方法解决了我的问题。如果可以的话,我会投10次赞成票! - spikyjt
1
@AndreiLED,你可以通过编辑/添加.git/info/attributes来针对每个仓库覆盖此设置。优先顺序为全局配置->目录中的.gitattributes->.git/info/attributes(即后者具有最高优先级并覆盖其他设置)。参考链接:http://git-scm.com/docs/gitattributes - spikyjt
谢谢,我会尝试一下。不过,我已经通过将core.eol设置为lf来实现了所需的效果。 - Andrei LED

7
至少在现代版本的git中,.git/info/attributes(或$GIT_DIR/info/attributes)会覆盖.gitattributes进行本地配置。
使用* !text来使用core.autocrlf的值,或者使用* -text来强制不进行转换。
请参阅gitattributestext属性的文档。
还要注意:core.eoleol属性

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接