How-to: Update the email address in a Git commit

If you accidentally made a Git commit with the wrong email address, you can easily update it afterwards by customizing and then executing this Bash script:

#!/bin/bash

# Change the OLD_EMAIL to be the NEW_EMAIL, only affecting the commits in REV_LIST
: ${OLD_EMAIL:=OLD_EMAIL@ADDRESS.COM}
: ${NEW_EMAIL:=YOUR_EMAIL@ADDRESS.COM}
: ${REV_LIST:=origin/master..HEAD}

git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ];
  then export GIT_AUTHOR_EMAIL=$NEW_EMAIL; export GIT_COMMITTER_EMAIL=$NEW_EMAIL;
  fi; git commit-tree "$@"' $REV_LIST

Here's an example to update the email address from wrong@gmail.com to right@gmail.com in the last two commits:

#!/bin/bash

# Change the OLD_EMAIL to be the NEW_EMAIL, only affecting the commits in REV_LIST
: ${OLD_EMAIL:=wrong@gmail.com}
: ${NEW_EMAIL:=right@gmail.com}
: ${REV_LIST:=HEAD~2}

git filter-branch --commit-filter 'if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ];
  then export GIT_AUTHOR_EMAIL=$NEW_EMAIL; export GIT_COMMITTER_EMAIL=$NEW_EMAIL;
  fi; git commit-tree "$@"' $REV_LIST

Explore More