过程是, 我先提交了一个commit AAA到gerrit, change-id=MMM, Ethel cherry-pick
了我的commit, 修改后继续提交, 生成了新的commit id=BBB, change-id依然是MMM. 我再cherry-pick
以后, 发现我的git log
里有两个相同的commit, commit message一样, change id一样, 但是commit id不一样.
这时修改之前, 先要做一次squash操作把两次commit合并成一个, 然后再修改, 提交,
否则的话, 如果直接在最新的commit上修改并push, gerrit上会产生一个
squash commits first
的错误.
合并commit步骤如下, 假设有两个相关的commit AAA和BBB, 同时还需要AAA的上一个commit CCC
commit BBB
commit AAA
commit CCC
使用命令git rebase -i CCC
或者git rebase -i HEAD~2
必须通过
CCC
才能合并AAA
和BBB
此时出现如下界面
ezuonli@esekilxxen3009:/repo/ezuonli/sync> git rebase -i HEAD~2
pick a17cb5e DO:WP5853 - CleanCode Chapter 2: meaningful names
pick 7f82077 DO:WP5853 - CleanCode Chapter 2: meaningful names
# Rebase 27055de..7f82077 onto 27055de (2 command(s))
#
# 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 7f82077 DO:WP5853 - CleanCode Chapter 2: meaningful names
中的pick换成squash
, 表示实用第一行的commit.
pick a17cb5e DO:WP5853 - CleanCode Chapter 2: meaningful names
suqash 7f82077 DO:WP5853 - CleanCode Chapter 2: meaningful names
然后进入编辑commit message阶段. 因为两个commit的commit message和change id都相同, 所以保留一个就可以了. :wq
退出, 这时两个commit合并成了一个, 并且生成了一个新的commit id DDD
commit DDD
commit CCC
下面就可以正常push了