The other day I made about 30 commits to a project without realizing my editor was adding spaces to the files even though they mostly contained tabs. I really couldn’t care less whether files have tabs or spaces, but consistency is the important thing, so I decided to rewrite my history to convert them using this command:
git filter-branch –tree-filter “git diff –name-status master HEAD | egrep ^[AM] | grep .cs | cut -f2 | xargs sed -i \”s/ /\t/g\”” master^..HEAD
git rebase master
Git basically checks out every revision of the repo into a directory. I then use git diff to check which “.cs” files were new or modified in the range I’m rewriting, which I then pipe to sed and replace four spaces with a tab. The reason I go from “master^” is so that rebasing onto master will have a commit at the start which normalizes the endings of all the files I’m about to change – though you’ll probably want to reword that commit with an interactive rebase later!
Hopefully you can see the four spaces and \t which can be flipped round to do the opposite conversion.