indentation configuration only for some 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
  • Match files using the syntax for .gitignore files. i.e. * matches everything and [ch] matches both characters c and h.
  • The whitespace option lists things to warn for.
    • Setting the 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!
      Add space-before-tab to catch spaces hidden before and between the tabs.
      Note that spaces following tab are
  • Aside: diff=cpp enables smarter diffs for C and C++ files.
  • Aside 2: trailing-space warns on trailing white space characters at the end-of-line and end-of-file.

Verify Rules

  • To verify what rules from .gitattributes are applied use:
git check-attr --all -- <pathname>

<pathname> does not have to be an existing file. (i.e. some.cpp works)

  • To test the whitespace rules:
    Create a dummy file matching the filename rule and call: git diff.
trailing space 
trailing tab    
  2 spaces
    4 spaces
    tab
     tab and space
    space and tab
        tab, space, tab

Usage

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]

Tags:

Git