通过--amend和rebase修改git commit提交的注释信息

简述

开发过程中,可能因为某些原因需要修改已经commit的内容。

最近一次提交

  1. 执行git commit --amend,进入注释编辑界面。
  2. 修改commit,保存提交。

倒数第n次提交

这个不好描述,以示例来说明。

通过git log --oneline得到最近的提交如下:

007d82c (HEAD -> master) add:最近一次提交,即: HEAD~1
e046fb1 add:倒数第2次提交,即: HEAD~2
10992f6 haha:倒数第3次提交,即: HEAD~3
a538e21 add:倒数第4次提交,即: HEAD~4
....

倒数第3次提交,因为粗心,将add写成了aad

通过git rebase -i HEAD~3进入rebase界面:

pick 10992f6 haha:倒数第3次提交,即: HEAD~3
pick e046fb1 add:倒数第2次提交,即: HEAD~2
pick 007d82c add:最近一次提交,即: HEAD~1

# Rebase a538e21..007d82c onto a538e21 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

将目标行的pick修改为edit或者reword,并修改后面的commit内容,修改完成之后如下:

edit 10992f6 add:倒数第3次提交,即: HEAD~3
pick e046fb1 add:倒数第2次提交,即: HEAD~2
pick 007d82c add:最近一次提交,即: HEAD~1

# Rebase a538e21..007d82c onto a538e21 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

保存退出,完成修改。

**特别提示:**可以把HEAD~N中的N设置的大些,只要能包含目标提交即可。

修改多次提交

情景一:这些错误提交相互间隔较近

HEAD~N中的N设置的大些,一次性把错误的提交都包含,通过一次rebase完成修改。

情景二:这些错误提交很分散

通过多次rebase完成修改。

你可能感兴趣的:(Git)