Skip to content

Instantly share code, notes, and snippets.

@Markus-de-Koster
Last active April 4, 2024 18:37
Show Gist options
  • Select an option

  • Save Markus-de-Koster/1815eab4315813b41b684c7fa89ec005 to your computer and use it in GitHub Desktop.

Select an option

Save Markus-de-Koster/1815eab4315813b41b684c7fa89ec005 to your computer and use it in GitHub Desktop.
Change all occurrences of a specific email address in git commits to a new one. Helps preventing exposure of private email addresses after committing with wrong config.
#!/bin/bash
echo "Enter the new email address:"
read CORRECT_EMAIL
OLD_EMAILS=""
first=true
while true; do
echo "Enter an old email address to replace (or press ENTER to continue):"
read OLD_EMAIL
if [ -z "$OLD_EMAIL" ]; then
break
fi
if [ "$first" = true ]; then
OLD_EMAILS="$OLD_EMAIL"
first=false
else
OLD_EMAILS="$OLD_EMAILS $OLD_EMAIL"
fi
done
if [ -z "$OLD_EMAILS" ]; then
echo "No old email addresses provided. Exiting."
exit 1
fi
git filter-branch -f --env-filter "
CORRECT_EMAIL='$CORRECT_EMAIL'
OLD_EMAILS='$OLD_EMAILS'
for OLD_EMAIL in \$OLD_EMAILS; do
if [ \"\$GIT_COMMITTER_EMAIL\" = \"\$OLD_EMAIL\" ]; then
export GIT_COMMITTER_EMAIL=\"\$CORRECT_EMAIL\"
fi
if [ \"\$GIT_AUTHOR_EMAIL\" = \"\$OLD_EMAIL\" ]; then
export GIT_AUTHOR_EMAIL=\"\$CORRECT_EMAIL\"
fi
done
" --tag-name-filter cat -- --branches --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment