Git 修改历史提交人(committer)和作者(author)的信息
经常性更改个人邮箱,每次修改邮箱后,之前的提交的信息就与本人不同,会导致项目出现多个协作者,非常不爽。有以下几种解决方式。
方式一: 仅修改当前分支。GitHub 会显示为当前日期(Push Date),可以签名(GPG 签名)
方式二:会修改所有分支。GitHub 不会修改为当前日期,但是无法签名(GPG 签名),可配合 git filter-branch
工具签名。
方式一:使用原生的 Git 命令修改
查看历史提交

$ git log
commit 37b827e68c9f24402a7053061a547540cb3afea3 (HEAD -> main)
Author: hello <hello@outlook.com>
Date: Fri Aug 28 00:16:36 2020 +0800
init
修改记录
只修改最近一次提交人信息
git commit --amend --author="New Name <new.email@example.com>" --no-edit

修改所有的历史记录

# 所有的历史记录
# 默认情况下,Git 会更新提交的日期。
git rebase -i --root
# 进入 VI 界面后,将所有记录 `commit` 前面的 `pick` 修改为 `edit`
# vi 快捷命令
:%s#pick#edit#g
# 保存
:wq
# 循环执行,直至最后报错
git commit --amend --author="New Name <new.email@example.com>" --no-edit
git rebase --continue
# 使用 shell while 写个循环操作,直至其报错自动退出
while true; do git commit --amend --author="New Name <new.email@example.com>" --no-edit && git rebase --continue || break; done
强制更新
# 强制更新
git push --force --all
# 查看指定的信息,记录下对应的 commit id
git log --author="old.email@example.com" --oneline
(注意,会清空 git repo url,需要先记录下来)
# 查看 repo url
git remote -v
本站介绍
根据条件指定条件
# 修改 author 信息
git filter-repo --commit-callback '
if commit.author_email == b"old.email@example.com":
commit.author_name = b"New Name"
commit.author_email = b"new.email@example.com"
' --force
# 修改 committer 信息
git filter-repo --commit-callback '
if commit.committer_email == b"old.email@example.com":
commit.committer_email = b"New Name"
commit.committer_email = b"new.email@example.com"
' --force
# 修改全部
git filter-repo --force --commit-callback '
if commit.committer_email == b"old.email@example.com":
commit.author_name = b"New Name"
commit.author_email = b"new.email@example.com"
commit.committer_email = b"New Name"
commit.committer_email = b"new.email@example.com"
'
重新签名(打 GPG 签名)
git filter-branch -f --commit-filter '
git commit-tree -S "$@";
' --tag-name-filter cat -- --branches --tags
恢复 remote url
git remote add origin git@github.com:repo/example.git
强制更新
# 强制更新
git push --force --all
打印历史记录
git filter-repo --commit-callback '
from pprint import pprint
pprint(commit.__dict__)
print("----")
'