为了标准化开发人员的平台,我的一个需求是提交 .git/config
,以便每个人都具有相同的CRLF配置,而不会忘记手动设置。
如何设置?
我有点担心所有这些反对 autocrlf
的情绪。如果它不起作用,为什么不删除这个功能?无论是该功能的制作者被误解了还是他们在实验中失败了,都应该将其删除,以防止更多人浪费时间(阅读晦涩的man页面,提问,回答这些问题的人等)。
为了标准化开发人员的平台,我的一个需求是提交 .git/config
,以便每个人都具有相同的CRLF配置,而不会忘记手动设置。
如何设置?
我有点担心所有这些反对 autocrlf
的情绪。如果它不起作用,为什么不删除这个功能?无论是该功能的制作者被误解了还是他们在实验中失败了,都应该将其删除,以防止更多人浪费时间(阅读晦涩的man页面,提问,回答这些问题的人等)。
我一直发现autocrlf
配置属性有问题(如我在Git 1.6.4 beta on Windows (msysgit) - Unix or DOS line termination中的回答所述):
注意:msysgit 问题538建议将其默认设置为false,而不是true(这是当时msysgit安装程序的默认值——2010年!),但我并不认同。
以下是我喜欢的三种解决方案之一:
第一步:git config --global core.autocrlf false
然后:
1. 使用新的core.eol
配置设置(1.7.2+)
设置工作目录中具有文本属性设置的文件要使用的行结束类型。
可选的值包括 'lf
'、'crlf
' 和 'native
',其中 'native' 使用平台的本地行结束符。默认值为 native。
2. 检出/检查.gitattribute
文件。请参见gitattributes手册:crlf
或 core.autocrlf
是将先前的本地配置属性记录在.gitattributes
文件中的方法。
您可以添加检出/签入属性,例如:
*.vcproj text eol=crlf
*.sh text eol=lf
3. 一个git属性过滤器驱动程序,可以:
.gitattributes
)记录,能够被推送到任何地方。\n
”,并且不更改任何关于行尾的内容。但是如果您要强制执行一种行尾样式,那么.gitattributes
非常有趣,因为您可以将其应用于整个存储库,或仅应用于存储库中的特定部分。 - VonCcore.autocrlf = false
,.gitattributes 中有 *.rb text
,那么它不会起作用吗? - vrish88text
意味着它将遵循所有文本文件(在您的 .git/config
中)的 core.eol
配置变量。 - VonC如果您使用的是Unix家族操作系统,我建议只需创建符号链接。
ln -s .git/config git-config
git add git-config
git commit -m "Now tracking git config file"
.git/config
可以被~/.gitconfig
本地覆盖。
因此,在构建、Makefile或配置脚本的一部分中,您可以为用户提供在其~/.gitconfig
中进行更改的建议,或通过git config
加载本地脚本.gitconfig
。
例如,创建带有某些设置的新的.gitconfig
,并通过以下方式加载它:
git config --local include.path "/path/to/.gitconfig"
或者要求用户在~/.gitconfig
文件中添加以下内容:
[include]
path = .gitconfig
Vagrantfile
加载git配置:system('GIT_TRACE=1 git config --local include.path "$(git rev-parse --show-toplevel)/git/gitconfig"');
然后在 git/gitconfig
中提交你的 git 配置文件,这样每次用户运行他们的 VM 时,该文件将自动加载到他们主机上的 git 中(例如,强制禁用 core.filemode
,以便 Windows 不会出现文件权限问题)。
要强制用户使用特定的换行符号,请改用 .gitattributes
文件,它应该可以直接使用。以下是使用类 Unix 的换行符(LF
)的示例语法:
# Drupal git normalization
# @see https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
# @see https://www.drupal.org/node/1542048
# Define text file attributes.
# - Treat them as text.
# - Ensure no CRLF line-endings, neither on checkout nor on checkin.
# - Detect whitespace errors.
# - Exposed by default in `git diff --color` on the CLI.
# - Validate with `git diff --check`.
# - Deny applying with `git apply --whitespace=error-all`.
# - Fix automatically with `git apply --whitespace=fix`.
*.css text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.html text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=html
*.js text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
*.json text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2
# Auto-detect text files, ensure they use LF (not recommended).
#* text=auto eol=lf
# Define binary file attributes.
# - Do not treat them as text.
# - Include binary diff in patches instead of "binary files differ."
*.gz -text diff
也许更好的方法是使用硬链接:
在*nix或OS X系统中:
ln .git/config git-config
git add git-config
git commit -m "Now tracking git config file"
在NTFS文件系统下的Windows系统中:
mklink /H git-config .git\config
git add git-config
git commit -m "Now tracking git config file"
git clone FROM_PROJ_URL
rm .git/config
ln git-config .git\config
在NTFS文件系统上的Windows操作系统中:
git clone FROM_PROJ_URL
del .git\config
mklink /H .git\config git-config