我想用
git config core.whitespace tab-in-indent,tabwidth=4我想要为c++文件设置这些设置,这样当我使用git diff时,当有错误的缩进时,我就会收到警告。然而,我也有需要标签的Makefile。有没有办法为不同的文件配置不同的空格设置?
发布于 2012-12-11 23:26:55
可以使用gitattributes调整这些设置。下面是我的.gitattributes文件的一个片段:
*.c text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.cpp text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.h text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.hpp text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.py text diff=python whitespace=trailing-space,space-before-tab,tab-in-indent
*.tex text diff=tex whitespace=trailing-space,space-before-tab,tab-in-indent
*.java text diff=java whitespace=trailing-space,space-before-tab,tab-in-indent
*.pl text diff=perl whitespace=trailing-space,space-before-tab,tab-in-indent
*.php text diff=php whitespace=trailing-space,space-before-tab,tab-in-indent
*.rb text diff=ruby whitespace=trailing-space,space-before-tab,tab-in-indent
*.vcproj eol=crlf
*.dsp eol=crlf
*.dsw eol=crlf
*.sh eol=lf
*.jpg binary
*.png binary
*.gif binary
*.tiff binary要调整您的空格设置,您可以使用如下内容:
*.ext whitespace=tab-in-indent,tabwidth=4*.ext可以指向路径,包含globs等等,这是一个非常灵活的机制。
发布于 2019-07-29 03:02:28
John Szakmeister和UpAndAdam对答案的一些补充。
要设置特定于文件的规则,您必须在项目的根目录中添加一个.gitattributes文件。docs
(如果您不希望它是受版本控制的,可以将其添加为:.git/info/attributes。)
# Macro's
[attr]cpp diff=cpp whitespace=trailing-space,space-before-tab,indent-with-non-tab,tabwidth=4
[attr]makefile whitespace=trailing-space,indent-with-non-tab,space-before-tab,tabwidth=4
*.[ch] cpp
*.[ch]pp cpp
makefile makefile
s.makefile makefile*匹配所有内容,而[ch]同时匹配字符c和h。whitespace选项列出了需要警告的内容。whitespace选项中设置tabwidth用于确定何时以及如何替换制表符和空格字符。(默认值为8个字符。) tab-indent认为使用制表符进行缩进是错误的。当一行开头使用4个或更多空格时,indent-with-non-tab会在此处发出警告。请注意,3个空格的缩进是可以接受的!添加space-before-tab以捕获选项卡之前和选项卡之间隐藏的空格。
请注意,制表符后面的空格是
diff=cpp为C和C++文件提供了更智能的差异。处警告尾随空白字符的trailing-space
验证规则
要验证应用了.gitattributes中的哪些规则,请使用
git check-attr --all -- <pathname><pathname>不必是现有文件。(即some.cpp工作)
要测试whitespace规则,请使用
创建一个与文件名规则匹配的虚拟文件并调用:git diff.
trailing space
trailing tab
2 spaces
4 spaces
tab
tab and space
space and tab
tab, space, tab用法
调用git diff时标记问题,调用git -apply ---whitespace=[warn|error|fix]时检查/修复问题。使用以下命令配置git apply的默认行为:
git config --[global|local] apply.whitespace [warn|error|fix]发布于 2015-08-27 03:26:40
正如jszakmeister从我的评论中更新的那样,这里只是您所询问的问题的一个子部分。
注意'makefile-ish‘条目中-修饰符的用法,表示不要调用制表符缩进错误。
makefile text whitespace=-tab-in-indent,trailing-space,tabwidth=4
Makefile text whitespace=-tab-in-indent,trailing-space,tabwidth=4
*.mk text whitespace=-tab-in-indent,trailing-space,tabwidth=4
*.cpp text diff=cpp whitespace=tab-in-indent,trailing-space,tabwidth=4
*.hpp text diff=cpp whitespace=tab-in-indent,trailing-space,tabwidth=4https://stackoverflow.com/questions/13819764
复制相似问题