Skip to content

Instantly share code, notes, and snippets.

@esebastian
Created July 5, 2014 18:04
Show Gist options
  • Save esebastian/cc1bc8b1ce0259c037a0 to your computer and use it in GitHub Desktop.
Save esebastian/cc1bc8b1ce0259c037a0 to your computer and use it in GitHub Desktop.
Rewrite git history replacing both author and commiter data for a given matching email
#!/bin/sh
# Git script to rewrite history replacing both author and commiter data for a given matching email,
# based upon this nice gist: https://help.github.com/articles/changing-author-info
# As the original source states, this action is destructive to your repo's history.
# It's best to do this on a clone, just in case. Also beware that this should not be performed on
# a repo that has been shared with others without forcing them to reset their history.
#
# Use at your own risk.
#
# Put the script in your path and invoke it this way:
# git author <your@email.to.match> <your@new.email> <yourname>
valid_email="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
if [[ ! $1 =~ $valid_email ]] && [[ ! $2 =~ $valid_email ]] && [[ ! -n $3 ]]; then
echo "usage: git author <your@email.to.match> <your@new.email> <yourname>\n"
exit 1
fi
export old_email=$1
export new_email=$2
export new_name=$3
echo "Substituting $old_email by $new_email ($new_name)..."
git filter-branch -f --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "$old_email" ]
then
cn="$new_name"
cm="$new_email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$old_email" ]
then
an="$new_name"
am="$new_email"
fi
export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
[[ $? -eq 0 ]] && echo Done!
[[ $? -ne 0 ]] && echo Error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment