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

Aur*_*ron 5 git

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

原始存储库http://img532.imageshack.us/img532/9039/screenshotrm.png

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

我在找什么http://img684.imageshack.us/img684/9977/screenshot2ah.png

Cas*_*bel 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"
Run Code Online (Sandbox Code Playgroud)

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

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

# 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"
Run Code Online (Sandbox Code Playgroud)

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

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

要修改历史记录以查找您想要的方式,您可以执行以下操作:

# 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
Run Code Online (Sandbox Code Playgroud)