当前位置 博文首页 > 韩超的博客 (hanchao5272):通过--amend和rebase修改git commit
开发过程中,可能因为某些原因需要修改已经commit
的内容。
git commit --amend
,进入注释编辑界面。这个不好描述,以示例来说明。
通过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
完成修改。