批量修改 Git 提交记录中的名字和邮箱

首先切换到仓库的目录, 然后执行下面的语句:

# 请先替换脚本里的中文字
git filter-branch --env-filter '
an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"
if [ "$GIT_COMMITTER_EMAIL" = "原邮箱地址" ]
then
    cn="目标名字"
    cm="目标邮箱地址"
fi
if [ "$GIT_AUTHOR_EMAIL" = "原邮箱地址" ]
then
    an="目标名字"
    am="目标邮箱地址"
fi
    export GIT_AUTHOR_NAME="$an"
    export GIT_AUTHOR_EMAIL="$am"
    export GIT_COMMITTER_NAME="$cn"
    export GIT_COMMITTER_EMAIL="$cm"
'

执行时会弹出以下警告:

WARNING: git-filter-branch has a glut of gotchas generating mangled history
rewrites. Hit Ctrl-C before proceeding to abort, then use an
alternative filtering tool such as ‘git filter-repo’
(https://github.com/newren/git-filter-repo/) instead. See the
filter-branch manual page for more details; to squelch this warning,
set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch…

大致意思是:

警告:git-filter-branch 有太多的陷阱,会生成损坏的历史记录重写。
先按Ctrl-C以中止操作,然后使用其他过滤工具,例如 ‘git filter-repo’ (https://github.com/newren/git-filter-repo/)。
有关更多详细信息,请参见过滤器分支手册页。
要消除此警告,请设置FILTER_BRANCH_SQUELCH_WARNING = 1。

不用理会, 等几秒钟后会接着执行. 最终看到的结果形式如下:

Rewrite 380ed76b2376c0a75ce49d7ed9608a775e96635c (11/11) (0 seconds passed, remaining 0 predicted)
Ref ‘refs/heads/master’ was rewritten

再用 git log 命令查看提交记录即可发现名字与邮箱被改过来了, 任务完成.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注