Rewriting author information in Git commits

1 minute read

Ever needed to change your email address in your Git history? For instance, you’ve mistakenly used your personal email address in a repository at work? Here’s a quick tip about how to fix that by filtering your Git repository.

Filtering git

I keep forgetting how to do this, so here it is for posterity. I know I could look it up in the Git Book, however I can probably more easily remember that I put it here. :smiley:

Anyway, here’s the tip:

git filter-branch -f --commit-filter \
    'GIT_AUTHOR_EMAIL="<new-author-email-address>" \
     GIT_COMMITTER_EMAIL="<new-committer-email-address>" \
     git commit-tree "$@"' HEAD

The basic idea here is that we want to filter all commits in the repository and replace the author and committer email addresses with the correct email addresses. Usually the GIT_COMMITTER_EMAIL is the same as the GIT_AUTHOR_EMAIL, so one can just use the same value in <new-author-email-address> and <new-committer-email-address> above.

Here are the details:

  • the -f option forces the process because Git won’t work on an existing directory if it already exists.
  • --commit-filter defines the filter command to apply to each commit. Here we just set the metadata that we want to change and call commit-tree to update the tree’s information. The "$@" is bash syntax to slurp in all of the arguments that filter-branch passes to the commit-filter command.
  • HEAD specifies that we want to filter all commits in the repository. If we just want to filter (say) the last 5 commits, then we could use HEAD~5..HEAD in that case.

Note that this process rewrites the commits in the repository, therefore hopefully you’ve not yet pushed the commits that you’re going to rewrite. If you do already have commits in an upstream repository with the wrong email address and you want to fix that, then it’s best to discuss the situation with your colleagues as you’ll be changing their repositories as well once you’ve pushed the filtered changes upstream.

It’s a good idea to check that everything was updated as you expected. I like to use the command line tool tig to do this because it’s really fast and gives a very good overview of a repository.

If you’re happy that everything looks good, then it’s just a simple matter of pushing your changes upstream and you’re done!

Support

If you liked this post and want to see more like this, please buy me a coffee!

buy me a coffee logo

Categories:

Updated: