Fixing Email Addresses in Git Repos after migration from Mercurial using Fast Export

Migrating repos from Mercurial to Git can be achieved by a variety of methods. The best method I've found is to use fast-export (not HgGit), however regardless of the method  they all borked the importing of my email address on commits. In this post I'll detail how to fix this.

First I performed the conversion as detailed here.

After this all my commits where shown in gitk as devnull@localhost although this only came to my attention when I tried to push to github and got an invalid-email-address error.

This can be easily fixed using the git filter-branch command:

#!/bin/bash


git filter-branch -f --env-filter '

an='$GIT_AUTHOR_NAME'
am='$GIT_AUTHOR_EMAIL'
cn='$GIT_COMMITTER_NAME'
cm='$GIT_COMMITTER_EMAIL'

# Repeat this for each user / email which needs fixing
if [ '$GIT_AUTHOR_NAME' = '<Name used on commit>' ]
then
    cn='<Name used on commit>'
    cm='<New email address>'
    an='<Name used on commit>'
    am='<New email address>'
fi

export GIT_AUTHOR_NAME='$an'
export GIT_AUTHOR_EMAIL='$am'
export GIT_COMMITTER_NAME='$cn'
export GIT_COMMITTER_EMAIL='$cm'
' -- --all

Obviously the placeholders need to be replaced with your values.

This code is based on a stackoverflow answer but that only works for the current branch, mine applies to all branches.