如何在所有克隆/机器上禁用Git行尾(CRLF到LF)? [英] How to to disable Git end-of-line (CRLF to LF) across all clones/machines?
问题描述
正如人们可以从 其他 帖子,Git的行尾标准化有其优点和缺点。我有一个特定于Windows的项目,我认为最好的做法是完全禁用行结束标准化。也就是说,我想保留所有换行符(大部分都是 CRLF
),而不是让git将它们标准化为 LF $ c $仅仅在幕后,我希望这种改变会影响所有机器上所有存储库的克隆。问题是最有效的方法。
Git的行尾标准化大多数讨论都是以 core.autocrlf
,我可以通过设置 core.autocrlf = false
来实现我的目标。但是,这是一个git-config设置,我相信你必须在每台机器上分别设置它。如果这是真的,那似乎很容易出错,特别是自 msysgit
安装程序指导您设置 core.autocrlf = true
。
避免在每台机器上分别设置 core.autocrlf
的最好方法似乎是检查 .gitattributes
文件放入包含单行的存储库中
* -text
或者,如果您有旧版本的Git,那么
* -crlf
这告诉Git,对于所有路径(因此 *
),不应该尝试结束行标准化。据我所知,这应该不会有任何其他副作用。特别是,它不应该改变如何生成差异(它具有单独的属性 diff
/ -diff
)或合并(它有一个单独的属性 merge
/ -merge
)。
有关更多详细信息,我建议您使用以下资源:
- gitattributes文档(
git help attributes
或在线副本),其中详细描述了行结束标准化如何工作以及不同属性的特定影响。 (可能最相关的是text
,crlf
,diff
,merge
和binary
。) - Git邮件列表线程text属性是否仅用于 来指定行结束标准化行为,还是它有更广泛的含义? 2012年3月30日),它扩展了不同属性的含义,并澄清
-text
并不简单地表示这是一个二进制文件。
As one can glean from other posts, Git's end-of-line normalization has its pros and cons. I have one particular Windows-only project where I think the best thing to do is to is to disable end-of-line normalization altogether. That is, I want to leave all newlines (most of which are CRLF
) intact, rather than have git normalize them to LF
-only behind the scenes, and I want that change to affect all clones of the repository on all machines. The question is the most effective way to do it.
Most discussions of Git end-of-line normalization are in terms of core.autocrlf
, and I could accomplish my goal by setting core.autocrlf=false
. However, this is a git-config setting, and I believe one has to set that separately on each machine by machine. If true, that seems error prone, especially since the msysgit
installer guides one into setting core.autocrlf=true
.
The best way to avoid having to set core.autocrlf
separately on each machine seems to be checking a .gitattributes
file into the repository containing the single line
* -text
Or, if you have an older version of Git then
* -crlf
This tells Git that, for all paths (thus the *
), end-of-line normalization should not be attempted. As far as I can tell, this should not have any other side-effects. In particular, it should not alter how diffs are generated (this has separate attribute diff
/-diff
) or how merges are handled (this has a separate attribute merge
/-merge
).
For more details, I suggest these resources:
- The gitattributes documentation (
git help attributes
or an online copy) , which describes in detail both how end-of-line normalization works and the particular effects of different attributes. (Probably most relevant aretext
,crlf
,diff
,merge
, andbinary
.) - Git mailing list thread Is the "text" attribute meant only to specify end-of-line normalization behavior, or does it have broader implications? (Mar 30, 2012), which expands on the meaning of different attributes, and clarifies that
-text
does not mean simply "this is a binary file".
这篇关于如何在所有克隆/机器上禁用Git行尾(CRLF到LF)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!