Git 修改历史提交人(committer)和作者(author)的信息
经常性更改个人邮箱,每次修改邮箱后,之前的提交的信息就与本人不同,会导致项目出现多个协作者,非常不爽。有以下几种解决方式。
方式一: 仅修改当前分支。GitHub 会显示为当前日期(Push Date),可以签名(GPG 签名)
方式二:会修改所有分支。GitHub 不会修改为当前日期,但是无法签名(GPG 签名),可配合 git filter-branch 工具签名。
方式一:使用原生的 Git 命令修改
- 只修改最近一次提交人信息
git commit --amend --author="New Name " --no-edit
- 修改所有的历史记录
# 所有的历史记录
# 默认情况下,Git 会更新提交的日期。
git rebase -i --root
# 进入 VI 界面后,将所有记录 `commit` 前面的 `pick` 修改为 `edit`
# vi 快捷命令
:%s#pick#edit#g
# 保存
:wq
# 循环执行,直至最后报错
git commit --amend --author="New Name " --no-edit
git rebase --continue
# 或者,使用 shell while 写个循环操作,直至其报错自动退出
while true; do git commit --amend --author="New Name " --no-edit && git rebase --continue || break; done
- 强制更新
git push --force --all
方式二:使用第三方插件 git-filter-repo 修改
(注意,会清空 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
- *(可选)重新签名(打 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
pip install git-filter-repo