git修改历史提交记录用户名和邮箱

发布时间 2023-12-11 11:53:50作者: 护发师兄

1. 使用 git filter-branch 修改提交历史

git filter-branch --env-filter '
OLD_EMAIL="旧邮箱@example.com"
CORRECT_NAME="你的名字"
CORRECT_EMAIL="新邮箱@example.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

替换 <旧邮箱@example.com>、<你的名字> 和 <新邮箱@example.com> 为你需要的值。

2. 强制推送到远程仓库

git commit --amend --author="jonil <jonilchan@foxmail.com>" --no-edit