Skip to content

Modify usernames in existing Git commits in batches

About 254 wordsLess than 1 minute

Git

2024-11-28

For a long time, commits on GitHub were all made using work accounts, which caused confusion among committers in private repositories. I found a bash script in StackOverflow that can modify usernames in batches. I change it a bit and record it here.

There are two steps to modify:

  1. Execute the modification script
  2. Synchronize local modifications to the Git server

Run the script

rewrite.sh
#!/bin/sh

git filter-branch -f --env-filter '
OLD_EMAIL="your-old@email.com"
CORRECT_NAME="correct-git-username"
CORRECT_EMAIL="your-new@email.com"
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

Create a rewrite.sh file, modify OLD_EMAIL, CORRECT_NAME and CORRECT_EMAIL in the script as needed, and put the modified script in the root directory of the project to be modified and execute it.

bash ./rewrite.sh

Synchronize Git log Go to the server

Check the printed information. If the modification is successful, you can execute the command:

git log

This command can view the modification status of the existing commit information. If it is correct, execute git push -f to synchronize the modified content to the Git server.




Changelog

Last Updated: View All Changelog
  • docs: udpate docs

    On 12/1/24
  • fix(docs): update docs and add a new article

    On 11/28/24

Keep It Simple