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 using git’s filter-tree option.
This command checks out each original revision, and makes the changes specified, then commits that as a new revision. In this case I replace a tab with 4 spaces in every .cs file. I start from origin/master and go up to the currently checked out commit (head):
You can run an arbitrary script (perhaps run it from a different file if it’s longer than this though!). Here’s another example using 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 a tab with four spaces.
This one won’t deal well with files that are created anywhere except the first commit. I’ll leave that as an exercise for the reader 😉