I use tabs for indentation in my python programs, but I would like to collaborate (using git) with people who use spaces instead.
Is there a way for git to automatically convert between spaces and tabs (say, 4 spaces = 1 tab) on pushing/fetching? (similar to the CR/LF conversion)
If you are on windows then you have a few extra steps to get @Olivier Verdier's solution to work.
Here is the complete solution:
In your repository, add a file
.git/info/attributes
which contains:Linux/Unix
Now run the commands:
OS X
First install coreutils with brew:
Now run the commands:
All systems
You may now check out all the files of your project. You can do that with:
and all the python files will now have tabs instead of spaces.
Edit: changed the forced checkout command. You should commit your work first, of course.
Yes, one potential solution is to use a git attribute filter driver (see also GitPro book), to define a smudge/clean mechanism.
That way:
You can declare this filter driver (named here '
tabspace
') in the.git/info/attributes
(for a filter applied to all files within the Git repo), with the following content:Now run the commands:
See Olivier's answer for a concrete working example of such a smudge/clean set of instructions.
Very useful info for everyone using GitHub (or other similar service)
~/.gitconfig
Then I have two files:
attributes
and
attributes2
Working on personal projects
That way, when you finally push your work on github, it won't look silly in the code view with
8 space tabs
which is default behavior in all browsers.Contributing to other projects
That way you can work with normal tabs on
2 space indented
projects.Of course you can write similar solution for converting from
4 space to 2 space
which is the case if you want to contribute to projects published by me and you tend to use 2 spaces while developing.