Whitespace and tabs in Git

Today I had massive troubles setting up a github repository, having never done it before.

The main issue was git kept complaining on the pre-commit hook that loads of my files had trailing whitespace, and also that I’d mixed tabs and spaces. I will never do either of these again!

There were loads of solutions around, but none of them seemed to work for me, except the following two (I’m running Ubuntu 8.04)

find * -type f -not -path '.git' -print0 | xargs -0 sed -i -e "s/[[:space:]]*$//" find * -type f -not -path '.git' -exec sed -i 's/\t/ /g' {} \;

The first line removes all trailing whitespace from files, and the second converts tabs to spaces (If you want more than one space then just change the number of spaces in the second command).

Update: As per lamby‘s update below, these should read

find . -type f -not -path '.git' -print0 | xargs -0 sed -i -e "s/[[:space:]]*$//" find . -type f -not -path '.git' -exec sed -i 's/\t/ /g' {} \;

Thanks

Comments

Make comment

In general, you don’t want to use “find *” as your shell with expand the wildcard, not find. This results in any files starting with “.” (or files under directories starting with “.”) not being touched. (Which means your “-not -path ‘.git’” is pointless)

Just omit the wildcard or replace it with “.”. Alternatively, you can temporarily set ‘dotglob’.

Cool, thanks for that.

To be honest I was sort of playing around with different commands until something looked liked it worked. I’m a bit of a novice at the shell, and only really starting to get into real commands that do useful stuff.

Required. 30 chars of fewer.

Required.

captcha image Please, enter symbols, which you see on the image