- Script for batch modifying Git history commit author information, rewriting Git history records using
git filter-branch
.
You provided the script to modify the author information in the Git repository history in bulk. The overall approach is correct, but using an array (e.g., OLD_EMAILS=("...")
) within the git filter-branch
’s --env-filter
might cause compatibility issues because some shell environments (such as /bin/sh
) do not support array syntax.
To improve compatibility, it’s recommended to replace the array with space-separated strings and use a for
loop to iterate through each old email address. Here is an example of the modified script:
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAILS="TianlongXiang51@gmail.com nick@qq.com tianlongxiang51@gmail.com"
CORRECT_NAME="tianlong.xiang"
CORRECT_EMAIL="tianlong.xiang@foxmail.com"
for OLD_EMAIL in $OLD_EMAILS
do
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
done
' --tag-name-filter cat -- --branches --tags
Notes:
- It is recommended to back up your repository before executing this script to prevent any unexpected issues.
- This operation rewrites Git history, modifying the author information of commits, which may cause changes in commit hash values.
- If you have already pushed the changes to a remote repository, you need to use a forced push: Please use forced pushes cautiously, especially in collaborative projects, to avoid affecting others.
Count all unique authors’ email addresses in the repository
git log --format='%an <%ae>' | sort -u