git rebase master topic2
git branch -f topic1 HEAD~2 # On (rebased) topic2, set topic1 pointer
注意,这个假设topic1
只是一个指针的topic2
过去,即,不应该有上topic1
任何承诺使不上topic2
。 (HEAD~2
假定提交历史记录如图所示,实际上你可能想要使用一个特定的提交id。注意如果topic1
不存在,这也可以工作:因为它没有提交它的“own”,指针可以只是任意设定)
编辑:在这种情况下,则可以选择做:
git rebase master topic1
git rebase topic1 topic2
最终的结果应该是一样的第一个选项(IFF topic2
包含了所有的topic1
的承诺!)。此语法可能更易于理解,但如果topic1
确实包含的提交不在topic2
中,则解决方案将有所不同。如果是这种情况,前面的解决方案将简单地丢弃topic1
中的任何提交都不在topic2
中,而后者将合并它们到topic2
。这两种结果可能都是不理想的,但在我看来,第一种解决方案会更清楚会发生什么,这就是为什么我先把它放在第一位。
为了说明,如果你的提交历史类似:
a1 - a2 - a3 - a4 - a5 - a6 - a7 master
\
b1 - b2 - b3 - b4 - b5 topic2
\
c1 topic1
然后第一个解决方案(rebase
,branch
)会给你:
a1 - a2 - a3 - a4 - a5 - a6 - a7 - b1' - b2' - b3' - b4' - b5' topic2
\ master \ topic1
而第二个(rebase
,rebase
):
a1 - a2 - a3 - a4 - a5 - a6 - a7 - b1' - b2' - b3' - c1' - b4' - b5' topic2
\ master \ topic1
然而,在这种情况下,你可能想获得是:
a1 - a2 - a3 - a4 - a5 - a6 - a7 - b1' - b2' - b3' - b4' - b5' topic2
\ master \
c1' topic1
这一结果的解决办法是:
git branch tmp id_of_b3_commit # In this case id_of_b3_commit == topic1^
git rebase master tmp
git rebase tmp topic1
git rebase tmp topic2
git branch -d tmp
(如果制作成一个脚本这一点,你可以使用git merge-base topic1 topic2
来找到要提交到tmp
分支的提交的ID。)
它的工作原理。你能详细解释步骤2中会发生什么吗? – 2011-12-29 11:40:04
@bmm在步骤2中,移动分支“topic1”('-f'以强制设置现有分支)到当前位置过去两步的提交('HEAD',in“topic2”第一条命令让我们进入该分支)。就像它在答案中所说的那样,我会建议用日志中所需的提交ID替换'HEAD〜2'(即你想要的“id1”的id以分支)。 – Arkku 2011-12-29 15:07:26