Created
August 26, 2025 03:30
-
-
Save sjungling/f4efe0af1195c76dd5534bea4bba25f0 to your computer and use it in GitHub Desktop.
for when you forgot to update your git `user.email` for personal projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git-change-author-email() { | |
if [[ $# -ne 2 ]]; then | |
echo "Usage: git-change-author-email <old-email> <new-email>" | |
echo "Example: git-change-author-email [email protected] [email protected]" | |
return 1 | |
fi | |
local old_email="$1" | |
local new_email="$2" | |
# Check if the old email exists in git history | |
if ! git log --all --pretty=format:"%ae" | grep -q "^$old_email$"; then | |
echo "Warning: Email '$old_email' not found in git history" | |
echo "Existing author emails in history:" | |
git log --all --pretty=format:"%ae" | sort -u | |
return 1 | |
fi | |
echo "Changing author email from '$old_email' to '$new_email'" | |
echo "This will rewrite git history. Continue? (y/N)" | |
read -q "REPLY?> " | |
echo | |
if [[ "$REPLY" != "y" && "$REPLY" != "Y" ]]; then | |
echo "Aborted." | |
return 1 | |
fi | |
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --env-filter " | |
if [ \"\$GIT_AUTHOR_EMAIL\" = \"$old_email\" ]; then | |
export GIT_AUTHOR_EMAIL=\"$new_email\" | |
fi | |
if [ \"\$GIT_COMMITTER_EMAIL\" = \"$old_email\" ]; then | |
export GIT_COMMITTER_EMAIL=\"$new_email\" | |
fi | |
" --tag-name-filter cat -- --branches --tags | |
echo "Author email changed successfully!" | |
echo "Updated commit history:" | |
git log --oneline --pretty=format:"%h %an <%ae> %s" -5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment