Fixing Git Author Name and Email

After moving Mercurial repository to Git you might want to update user names and emails.

The first step would be to see the all the names:

Terminal
git log --format='%an <%ae>' | git log --format='%cn <%ce>' | sort | uniq

With this information in hand, we can adjust names with filter-branch:

Terminal
git filter-branch --force --commit-filter '
OLD_NAME="unknown"
NEW_NAME="My name"
NEW_EMAIL="myemail@example.com"
if [ "$GIT_AUTHOR_NAME" = "$OLD_NAME" ]; then
GIT_AUTHOR_NAME="$NEW_NAME"
GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_COMMITTER_NAME" = "$OLD_NAME" ]; then
GIT_COMMITTER_NAME="$NEW_NAME"
GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
git commit-tree "$@";
' --tag-name-filter cat -- --branches --tags --all

git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.