Fixing git commit emails using git filter-branch

29 Mar 2009

When I created the git repository for my Ruby algorithms library, I was a git noob and didn't properly set up my email in my .gitconfig. While this is fine, it makes it so that your activity graphs on Github don't reflect the period where you didn't set your email, since that's how Github rolls. Anyways, I ran the following script, then did a git push -f to do a force push to Github or any other remote repository.

If you don't force it, you will get ! [rejected] master -> master (non-fast forward), since you're updating past history.

#!/bin/sh

git filter-branch --env-filter '

an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
 
if [ "$GIT_COMMITTER_NAME" = "Kanwei Li" ]
then
cm="kanwei@gmail.com"
fi
if [ "$GIT_AUTHOR_NAME" = "Kanwei Li" ]
then
am="kanwei@gmail.com"
fi

export GIT_AUTHOR_EMAIL=$am
export GIT_COMMITTER_EMAIL=$cm
'