在git中将双父提交转换为单父提交

时间:2010-05-26 14:42:00

标签: git

糟糕!上次我在我的存储库中合并两个分支时似乎做错了什么:

Original repository http://img532.imageshack.us/img532/9039/screenshotrm.png

这远非我所期待的。有没有办法安排这个烂摊子并获得如下的东西?我做错了什么?

What I am looking for http://img684.imageshack.us/img684/9977/screenshot2ah.png

1 个答案:

答案 0 :(得分:11)

合并提交应该有两个父母。这就是合并的全部内容。

如果您有充分的理由想要只与一个父级合并,则可以使用--squash选项:

# starting with branch-full-reordering at "Corrected GPU float measures"
git merge --squash branch-full-reordering-wo-auxarray-wo-diver
# go on to make the "improve performance commit"

但这很少,很少你想要的。它将在其他分支上完成的所有工作转储到合并提交中,从而破坏历史记录。

但是,您描述的历史根本不是合并,壁球或其他。这是一个变种。

# starting with branch-full-reordering at "Corrected GPU float measures"
git rebase branch-full-reordering branch-full-reordering-wo-auxarray-wo-diver
# go on to make the "improve performance commit"

在这种情况下,将不会进行合并提交 - 您只需将辅助分支移植(重新绑定)到主要分支上。

我应该再次强调:你最终得到的历史可能就是你想要的。它记录了一些事实,即一些开发发生在一个侧支路上,然后被合并回来。

要修改历史记录以便按照您希望的方式进行修改,您可以执行以下操作:

# recreate your topic branch
git branch topic <SHA1 of commit "Correct pesky bug">
# overkill: you could also specify that as branch-full-reordering^^2
# which means the second parent of the [first] parent of the branch tip

# rebase the topic branch like you meant to in the first place :)
# ~2 means two commits before
# you could also just use the SHA1 of commit "Corrected GPU float measures"
git rebase branch-full-reordering~2 topic

# rebase the main branch onto the topic
# the merge will be ignored, so this will just move the "Improve performance" commit    
git rebase topic branch-full-reordering

# delete the topic branch
git branch -d topic