# How to Fix Wrong Commit Emails with git filter-branch

If you've made commits using the wrong email (e.g., personal email in a work repo), git filter-branch lets you rewrite history to fix them across your entire Git repository.

## TLDR; Fix All And Everywhere:

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

## What git filter-branch Does

This command rewrites Git history by creating a new set of commits that replace the old ones. You can use it to change commit metadata like author name and email, remove sensitive files, or modify commit messages.

## Warning

This rewrites history. After running this, you'll need to force-push to shared repositories:

git push --force --tags origin 'refs/heads/*'

Only use on repos where you understand the consequences and can coordinate with collaborators.