场景描述
当我们的某次 commit 中的邮箱和提交人不符合提交规范时(比如:用个人邮箱的配置提交到了公司的项目中等),我们需要修改这次 commit 的邮箱和提交人信息,需修改的 commit 可能是如下三种情形:
- 需修改的 commit 是当前分支最新 commit
- 需修改的 commit 是当前分支某历史 commit
- 需修改的 commit 是当前分支的第一个 commit
解决方式
我们一般使用 git commit --amend
命令来修改 commit 的相关信息,如果要修改历史提交,需要结合 git rebase
命令一起使用
我们通过 git commit --amend --help
查看下 git commit --amend
都有哪些额外参数:
所以,上述三种情形下的解决方式如下(git rebase 相关的操作解释可参考笔者这篇文档:如何修改第一个 commit 的提交信息):
需修改的 commit 场景 | 操作命令 |
---|---|
commit 是最新 commit | git commit --amend --author="newUser<newEmail>" --no-edit |
commit 是某历史 commit |
git rebase -i <father_commit> git commit --amend --author="newUser<newEmail>" --no-edit
|
commit 是第一个 commit |
git rebase -i --root git commit --amend --author="newUser<newEmail>" --no-edit
|
修改第一个 commit 邮箱和提交人实操演示
git log
显示提交信息如下:
git rebase -i --root
操作显示如下:
我们将左上角的 pick 改为 e, :wq
保存退出后显示如下:
我们将邮箱和提交人改为 xiaohu<xiaohu@qq.com>
, git commit --amend --author="xiaohu<xiaohu@qq.com>" --no-edit
执行后,显示如下:
git status
显示如下:
按照提示,进行 git rebase --continue
操作,显示如下:
git log
重新显示如下,邮箱和提交人信息已改变:
注意事项
如需将修改信息同步到远端仓库,可使用 git push -f
命令进行强制同步,该操作会覆盖远端分支的提交历史,请自行确认操作风险