cdruc

Change GIT user for past commits

It doesn't happen often, but when it happens, commiting without realizing the author information is wrong can be a pain in the ass to correct. You have to rebase, go through each commit, mark edit, write the correct user information, save.

But luckily, there is a better way.

You can rewrite the entire commit history, updating the user name and email on all branches and tags using the command bellow.

Important: only run this if you’re the only committer of the repository.

git filter-branch --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
Tags: