Programming‎ > ‎GIT‎ > ‎

Converting between tabs and spaces

It is very easy to convert between tabs and spaces automatically when committing and check-outing text files with GIT. GIT allows it with the attributes mechanism:

To configure this behavior all you need is:
  1. Place 'expand' and 'unexpand' commands in your 'PATH' environment variable. For Windows they are part of GnuWin32 CoreUtils package:

  2. Define needed GIT filter names. These are examples if you want tabs locally and two or four spaces in the repository:

    git config --system filter.tabspace2.clean "expand --tabs=2 --initial"
    git config --system filter.tabspace2.smudge "unexpand --tabs=2 --first-only"

    git config --system filter.tabspace4.clean "expand --tabs=4 --initial"
    git config --system filter.tabspace4.smudge "unexpand --tabs=4 --first-only" 

  3. Apply settings to your local repository.

    Create .git/info/attributes file inside your local repository directory and add following line(s) to it:

    *.<extension> filter=tabspace2

    Where <extension> is file extension than will be filtered.

  4. If you have any changes, commit them and then checkout all files to have filtered version on a disc:

    git checkout HEAD -- **


Things may be a little more complicated when you want to do the same with Nemerle source codes, because there is a risk that 'unexpand' command can corrupt files that are using indentation-based syntax. The solution is to use script that expands/unexpands only files with non indentation-based syntax. You can copy script to the bin directory of git and configure filters that way:

git config --system filter.tabspace2.clean " 2 expand"
git config --system filter.tabspace2.smudge " 2 unexpand"

Marek Ogarek,
Jul 22, 2012, 10:59 AM