Friday, March 24, 2017

Changing author and committer info in Git histroy

The script can be found on the Git official help pages.

I added the -f argument to the command to force rewrite existing backups, which would otherwise fail the script run.

#!/bin/sh

git filter-branch -f --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

DO NOT REWRITE THE HISTORY OF YOUR GIT REPOSITORY IF IT WAS ALREADY PUSHED AND IS AVAILABLE FOR COLLABORATORS!