Skip to content

Instantly share code, notes, and snippets.

@kmikko
Created November 23, 2021 10:53
Show Gist options
  • Save kmikko/68f93175c67887a221608848a44308d0 to your computer and use it in GitHub Desktop.
Save kmikko/68f93175c67887a221608848a44308d0 to your computer and use it in GitHub Desktop.
Changing the Git history of your repository using a script

https://help.github.com/en/github/using-git/changing-author-info

Changing author info

To change the name and/or email address recorded in existing commits, you must rewrite the entire history of your Git repository.

Warning: This action is destructive to your repository's history. If you're collaborating on a repository with others, it's considered bad practice to rewrite published history. You should only do this in an emergency.

Changing the Git history of your repository using a script

We've created a script that will change any commits that previously had the old email address in its author or committer fields to use the correct name and email address.

Note: Running this script rewrites history for all repository collaborators. After completing these steps, any person with forks or clones must fetch the rewritten history and rebase any local changes into the rewritten history.

Before running this script, you'll need:

  • The old email address that appears in the author/committer fields that you want to change
  • The correct name and email address that you would like such commits to be attributed to
  1. Open Terminal.

  2. Create a fresh, bare clone of your repository:

git clone --bare https://github.com/user/repo.git
cd repo.git
  1. Copy and paste the script, replacing the following variables based on the information you gathered:
    • OLD_EMAIL
    • CORRECT_NAME
    • CORRECT_EMAIL
#!/bin/sh

git filter-branch --env-filter '

OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"

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
  1. Press Enter to run the script.

  2. Review the new Git history for errors.

  3. Push the corrected history to GitHub:

git push --force --tags origin 'refs/heads/*'
  1. Clean up the temporary clone:
cd ..
rm -rf repo.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment