Modify usernames in existing Git commits in batches
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:
- Execute the modification script
- Synchronize local modifications to the Git server
Run the script
#!/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.
References for this article
Copyright
Copyright Ownership:dingyuqi
This article link:/en/article/utvqql9z/
License under:Attribution-NonCommercial-NoDerivatives 4.0 International (CC-BY-NC-ND-4.0)