修改Git历史提交记录的提交者名称和邮箱

来源:https://stackoverflow.com/questions/750172/how-to-change-the-author-and-committer-name-and-e-mail-of-multiple-commits-in-gi

修改旧邮箱OLD_EMAIL,新姓名CORRECT_NAME,新邮箱CORRECT_EMAIL,执行此脚本

此方法虽然可以达到效果,但使用git-filter-branch会对历史记录重写产生明显的干扰,所以需要慎用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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
git push --force