2 Git Rewrite User
tengel edited this page 2024-09-05 08:27:27 -05:00

Rewrite git history to change a username/email

#!/usr/bin/env bash
#
# Rewrite git commit history to replace user/email values
# - alters commit history, breaks linked clones/forks/references!
#
## USE WITH CAUTION

export FILTER_BRANCH_SQUELCH_WARNING=1

git filter-branch -f --env-filter '
WRONG_EMAIL="olduser@gmail.com"
NEW_NAME="newuser"
NEW_EMAIL="newuser@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$NEW_NAME"
    export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$NEW_NAME"
    export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags