GIT TIPS: Changing Email Addresses Globally

Another common case is that you forgot to run git config to set your name and email address before you started working, or perhaps you want to open-source a project at work and change all your work email addresses to your personal address. In any case, you can change email addresses in multiple commits in a batch with filter-branch as well. You need to be careful to change only the email addresses that are yours, so you use --commit-filter:

$ git filter-branch --commit-filter '
    if [ "$GIT_COMMITTER_EMAIL" = "[email protected]" ]; then
        GIT_COMMITTER_NAME="Scott Chacon";
        GIT_COMMITTER_EMAIL="[email protected]";
    fi;
    if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]; then
        GIT_AUTHOR_NAME="Scott Chacon";
        GIT_AUTHOR_EMAIL="[email protected]";
    fi;
    git commit-tree "[email protected]"' HEAD

This goes through and rewrites every commit to have your new address. Because commits contain the SHA-1 values of their parents, this command changes every commit SHA-1 in your history, not just those that have the matching email address.

See: 7.6 Git Tools - Rewriting History

Edit history:

  • First publish @2016-02-25 23:32:56 -04:00
  • Updated bash script @2017-04-21 14:32:40 -04:00
  • Fixed bash script @2017-08-15 05:34:04 -04:00
  • Fixed bash script @2018-07-05 08:37:12 -04:00