How to Change the User for All Your Git Commits

Over the course of a long-lasting project, your author information may change and the associations to a user profile on GitHub or GitLab may become broken. This is the ultimate guide for how to change the entire commit history.

Image for post
Image for post
Photo by Yancy Min on Unsplash

Rewrite All Affected Commits

$ git filter-branch --env-filter '
OLD_EMAIL="old@example.com"
NEW_NAME="New Example"
NEW_EMAIL="new@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Check the Updated Log

Push Rewritten History

$ git push origin --force --all
$ git push origin --force --tags

Common Issue in GitLab

Image for post
Image for post
Unprotect master branch in the GitLab repository settings.

Update All Clone Repositories

$ git fetch origin 
$ git reset --hard origin/master
$ git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
$ git reflog expire --expire=now --all
$ git gc --prune=now

Multiple Author Changes in Commit History

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

Remember to Set Author Information

$ git config user.name "New Example"
$ git config user.email "new@example.com"

Conclusion

Portfolio manager at Hagemann Capital. I invest in underdogs before they become megabrands.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store