Rewriting Git history

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.

Advertisements

About GrahamTheCoder

I'm Graham (the coder). A C# developer based in Cambridge hoping blogging will achieve some or all of the following. Help me organise my thoughts. Practice writing things other than code. Give me a place to refer people to when I'm trying to make a long winded point. I welcome comments and constructive criticism, and hope to look back at my written opinions in the future and laugh at my own naivety.
This entry was posted in Note to self. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s