跳到主要内容
  1. 所有文章/

批量重写 Git 历史提交记录

·644 字·约 2 分钟

我有一些 Git 存储库,需要批量重写历史提交记录来修改提交者名称和邮箱地址,同时保持历史提交时间不变。

参考 批量修改 Git 仓库的提交邮箱 - 宝硕博客,可以使用 git 的 filter-branch 命令,此外在重写提交记录过程中,我还会用 GPG 签名 Git 提交,因此有了下面的 Shell 脚本

#!/bin/bash

# set the committed user information to be replaced
old_user="old userName"
old_email="old@mail.com"
new_user="new userName"
new_email="new@mail.com"

# get global gpg signing key
signingkey=$(git config --global user.signingkey)

# rewrite the git commit history and sign new commits
git filter-branch --env-filter "
    if [ \"\$GIT_AUTHOR_EMAIL\" = \"$old_email\" ] && [ \"\$GIT_AUTHOR_NAME\" = \"$old_user\" ]; then
        export GIT_AUTHOR_EMAIL=\"$new_email\"
        export GIT_AUTHOR_NAME=\"$new_user\"
        export GIT_COMMITTER_EMAIL=\"$new_email\"
        export GIT_COMMITTER_NAME=\"$new_user\"
        export GIT_COMMITTER_SIGNINGKEY=\"$signingkey\"
        export GIT_AUTHOR_SIGNINGKEY=\"$signingkey\"
    fi
" --tag-name-filter cat --commit-filter 'git commit-tree -S "$@"' -- --branches --tags

修改 old_userold_emailnew_usernew_email 为修改前后的提交者信息,将脚本放到 Git 存储库的根目录下,然后就可以执行了(注意提前备份存储库)

chmod +x batch-edit-git-commit-logs.sh
./batch-edit-git-commit-logs.sh

可以看到 Git 警告:git-filter-branch 有大量的问题,如果操作不当,可能会产生错误的历史重写。另外,它推荐我们使用 git-filter-repo 作为替代品。

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...

Rewrite 606d7eb7c36d6d6426c613ba1869014784c9a383 (1/24) (0 seconds passed, remaiRewrite 270ce2df6567020343943fc24d3290df1c6ca3e8 (2/24) (0 seconds passed, remaiRewrite f2ae7427af8e72c9197e60dec58aa1504fcaa11b (3/24) (0 seconds passed, remaiRewrite a68f142368002e404943cf386a98bc289162b8e0 (4/24) (1 seconds passed, remaiRewrite 2e1e2e913015974ee1eaef66d66aa82b4562a95b (4/24) (1 seconds passed, remaiRewrite dad9470284f029b1ec1e91d7c5288d8bd306a64a (4/24) (1 seconds passed, remaiRewrite bf1f35e51ee5f123276e166c90408521d2f23ff7 (4/24) (1 seconds passed, remaiRewrite b8912cb89df508094dc11da3252e475b590f67a7 (4/24) (1 seconds passed, remaiRewrite 9f41378e3b8d431fea6c10568b9b4df6ac1936d5 (9/24) (1 seconds passed, remaiRewrite 0fcb83594814e374078680fd181f42e29dd1a8a0 (9/24) (1 seconds passed, remaiRewrite 7d9402380dfe35682d279b01b99097d4b220d2f4 (9/24) (1 seconds passed, remaiRewrite b074bb6230e21dd3415b1c18be1c0c604c21eaa2 (9/24) (1 seconds passed, remaiRewrite 2892fb7677d17c8d3a5ce561bdfcae46e3b9a20f (9/24) (1 seconds passed, remaiRewrite fe48751fc29a09870bc183c6541dbe21df50b04e (9/24) (1 seconds passed, remaiRewrite c1e06a3d0dbf8b667cdd8cf057ba3997556ad453 (9/24) (1 seconds passed, remaiRewrite 0f9bfcddd6bbccbe69d13c491b8f427276436579 (9/24) (1 seconds passed, remaiRewrite f44fba015a25f448d69115cf6ff96c7d169ffa8b (9/24) (1 seconds passed, remaiRewrite f05660f071161a44a8a8cf95e83230635e19f5f2 (9/24) (1 seconds passed, remaiRewrite e4b12f09ed13c5e06818c7d9fd207d9390a2922f (19/24) (3 seconds passed, remaRewrite f489360e133155210fb7a2eccf5800dc4b5df152 (19/24) (3 seconds passed, remaRewrite d48a7a81c223adab5df1c3eadc065aa652a6aa77 (19/24) (3 seconds passed, remaRewrite 89205fffd14961c01f537c1246de2e7955b626c1 (19/24) (3 seconds passed, remaRewrite 9a9ad88f4b06609f91d775ed6648aa4825a1965b (19/24) (3 seconds passed, remaRewrite a5ea05fe4de34cb93c9541477e887b04e29f0e11 (19/24) (3 seconds passed, remaining 0 predicted)
Ref 'refs/heads/master' was rewritten

git-filter-branch 的速度比较慢,此外,上面的脚本不适合重写含有多个提交者的存储库,它会使用 Git 全局配置的签名密钥对所有提交者的记录都进行签名,这是非常糟糕的。如果有更复杂的 Git 重写历史记录的需求,我们还是应当使用 git-filter-repo 工具。

Dejavu Moe
作者
Dejavu Moe
Not for success, just for growing.