Skip to content

Instantly share code, notes, and snippets.

@robin-hartmann
Last active June 7, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin-hartmann/16cc24b2ac7ae08ec1d68591915911a7 to your computer and use it in GitHub Desktop.
Save robin-hartmann/16cc24b2ac7ae08ec1d68591915911a7 to your computer and use it in GitHub Desktop.
Change the commit author name and email of all matching commits in a repository (e.g., for replacing outdated email addresses)
#!/bin/sh
# DON'T DO THIS, IF OTHER PEOPLE WORK ON YOUR REPO!
set -e
# TODO update these variables
REPO_TO_UPDATE='https://github.com/robin-hartmann/robin-hartmann'
WORKSPACE_FOLDER='workspace-change-commit-author'
git clone --bare $REPO_TO_UPDATE $WORKSPACE_FOLDER
cd $WORKSPACE_FOLDER
export FILTER_BRANCH_SQUELCH_WARNING=1
git filter-branch --env-filter '
# TODO update these variables
CORRECT_NAME="Robin Hartmann"
CORRECT_EMAIL="contact@robin-hartmann.com"
COMMITS_TO_SKIP=(
)
EMAILS_TO_REPLACE=(
old.name@old-domain.de
contact.old.name@old.domain.com
)
if [[ ! "${COMMITS_TO_SKIP[@]}" =~ "$GIT_COMMIT" ]]; then
if [[ "${EMAILS_TO_REPLACE[@]}" =~ "$GIT_COMMITTER_EMAIL" ]]; then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [[ "${EMAILS_TO_REPLACE[@]}" =~ "$GIT_AUTHOR_EMAIL" ]]; then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
fi
' --tag-name-filter cat -- --branches --tags
git push --force --tags origin 'refs/heads/*'
cd ..
rm -rf $WORKSPACE_FOLDER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment