2014-11-25 90 views
3

說,我有這種情況在我的git回購爲什麼我不能合併主題分支?

A--B--C--D master 
|\ 
| E--F  topic 
| 
\ 
    G--H  other 

,我想變基topicother分支。這工作正常:

git rebase --onto other master topic 

這給了我

A--B--C--D master 
| 
\ 
    G--H  other 
     \ 
     E--F topic 

但如果topic已經合併到主(git merge topic)以上rebase命令不工作了。如果我試試吧,我似乎得到這樣的:

A--B--C--D--M master 
|\  /
| E--F---- 
| 
\ 
    G--H   other/topic 

,而我想這一點:

A--B--C--D--M master 
|\  /
| E--F---- 
| 
\ 
    G--H   other 
     \ 
     E'--F' topic 

這是爲什麼,我怎麼可以變基topicother即使它已經合併master

編輯:更新底部圖表來清楚,EF是不相同的提交 - 但引入相同的變更集。

編輯2:我真正想要的是,彷彿我首先創建的topic副本(比如topic-rebased),我變基到other之前我合併topicother

git checkout topic 
git checkout -b topic-rebased 
git rebase --onto other master topic-rebased 
git checkout master 
git merge topic 

該作品精細。但是如果話題已經合併到master中,那麼rebase就不起作用了。

回答

3

您不能指望在您的存儲庫中存在提交中的每個EF提交中的兩個。如果他們有不同的父母(他們這樣做:一組有A作爲父母,另一組有H作爲父母),那麼一個E將是另一個不同的E

在你的情況,如果你想要這個結果,你應該看看cherry-picking

git checkout topic # puts you on `H` 
git cherry-pick E 
git cherry-pick F 

這將應用提交EF到當前分支,其本質就是它的副本。所以你最終應該得到理想的結果。

+0

好吧,我已經更新了一些問題。如果櫻桃採摘不是一種選擇,那麼因爲'topic'上有100多次提交,我還需要'其他'提交?請參閱上面的編輯2,瞭解我真正想要的內容。 – 2014-11-25 14:00:43

+0

我不太瞭解你的編輯,但是如果你需要櫻桃選擇許多提交(即E和F之間有更多的提交),你也可以選擇一個範圍。例如在'other'上使用'git cherry-pick A..topic'來挑選按主題引入的所有提交。 – poke 2014-11-25 14:16:08

+0

好的,但是我需要手動找出,從哪裏開始以及在哪裏結束。不知道,我的編輯還不清楚。下面的例子應該說得很清楚:如果我在*合併主題分支之前重新分配*,它會起作用。它不工作了,如果我試圖在主題合併後重新綁定。 – 2014-11-25 14:36:37

0

如果你的歷史,是在你的榜樣,這將做到這一點:

git checkout topic 
git rebase --onto other --root 

我這給了一杆,以確認:

% git init . 
(add commit a) 
% touch a; git add a ; git commit -m "a" 
% git checkout -b topic 
(add commits e and f, elided for brevity) 
% ls 
a e f 
% git checkout master 
% git checkout -b other 
(add commits g and h) 
% ls 
a g h 
% git checkout master 
(add commits b, c, and d) 
% ls 
a b c d 
% git merge topic 
Merge made by the 'recursive' strategy. 
e | 0 
f | 0 
2 files changed, 0 insertions(+), 0 deletions(-) 
% ls 
a b c d e f 
% git checkout topic 
% ls 
a e f 
% git rebase --onto other --root 
First, rewinding head to replay your work on top of it... 
Applying: e 
Applying: f 
% ls 
a e f g h 

它會自動找到共享的父母,並添加(真正的櫻桃選擇)只有主題分支的提交,即使它已經合併爲主。

相關問題