I want to use
git config core.whitespace tab-in-indent,tabwidth=4
I want to have these settings for c++ files, so that I get warnings when there are wrong indentations when I use git diff. However, I have also Makefiles which need tabs. Is there a way to configure different whitespace settings for different files?
You can use gitattributes
to tweak these settings. Here's a snippet of my .gitattributes file:
*.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
To adjust your whitespace settings, you could use something like:
*.ext whitespace=tab-in-indent,tabwidth=4
The *.ext
can point at paths, contain globs, etc. It's pretty flexible mechanism.
Some additions to the answers by John Szakmeister and UpAndAdam.
To set file-specific rules you'll have to add a .gitattributes
file in the root of you project. docs
(If you do not want it to be version controlled you can add it as: .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
*
matches everything and [ch]
matches both characters c
and h
. whitespace
option lists things to warn for.
tabwidth
in the whitespace
option is used to determine when and how to replace the tab and space characters. (The default value is 8 characters.) tab-indent
considers indentation using tabs an error. indent-with-non-tab
warns here when 4 or more spaces are used at the start of a line. Note that indentation with 3 spaces is accepted!space-before-tab
to catch spaces hidden before and between the tabs.diff=cpp
enables smarter diffs for C and C++ files.trailing-space
warns on trailing white space characters at the end-of-line and end-of-file..gitattributes
are applied use:git check-attr --all -- <pathname>
<pathname>
does not have to be an existing file. (i.e. some.cpp
works)
whitespace
rules:git diff
.trailing space
trailing tab
2 spaces
4 spaces
tab
tab and space
space and tab
tab, space, tab
Issues are marked when calling git diff
and checked/ fixed when calling git -apply ---whitespace=[warn|error|fix]
.
Configure default behaviour of git apply
with:
git config --[global|local] apply.whitespace [warn|error|fix]
As jszakmeister updated from my comment, here's just a sub-section for what you asked about.
Notice the usage of the -
modifier in the 'makefile-ish' entries to say don't call tab-in-indent an error.
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=4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With