I have separate emails for personal use and office use, so as my Git accounts. I use a small script to update my git config when I have to change workspace. But sometime I forget to do so (most of the time actually). So I end up making commit with wrong git account.

While search for solutions to fix this I found this awesome script, from Github,  that could change author email and name of existing commits. How cool is that!

So, I decided to keep it here :)

#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Update OLD_EMAIL , CORRECT_NAME and CORRECT_EMAIL as necessary. Just save it in a .sh file and run it. Thats it.

There is a chance that you might get a error like this:

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

In which case you have to remove the .git/refs/original/ directory.

rm -rf .git/refs/original

Review the git logs . And finally push them to repo:

git push --force --tags origin 'refs/heads/*'

Further reading