Skip to content

Instantly share code, notes, and snippets.

@NiklasPeterson
Last active May 25, 2021 07:45
Show Gist options
  • Save NiklasPeterson/de458516a2d0dae1eae73ad16f0baa5b to your computer and use it in GitHub Desktop.
Save NiklasPeterson/de458516a2d0dae1eae73ad16f0baa5b to your computer and use it in GitHub Desktop.
A collection of scripts that helps you change git author on commits

Changing git author info (for git commits)

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 https://hostname/user/repo.git
cd repo.git

3. List all git authors

git log --pretty=full | grep  -E '(Author|Commit): (.*)$' | sed 's/Author: //g' | sed 's/Commit: //g' | sort -u

4. Copy and paste the script, replacing the following variables based on the information you gathered:

  • OLD_EMAIL
  • CORRECT_NAME
  • CORRECT_EMAIL
git filter-branch --env-filter '

OLD_EMAIL="<old-mail>"
CORRECT_NAME="<correct-name>"
CORRECT_EMAIL="<correct-email>"

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 -f -- --all

5. Press Enter to run the script.

6. List all git authors agian & check so the information has been corrected

git log --pretty=full | grep  -E '(Author|Commit): (.*)$' | sed 's/Author: //g' | sed 's/Commit: //g' | sort -u

7. Review the new Git history for errors.

8. Push the corrected history to GitHub:

git push --force --tags origin 'refs/heads/*'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment