2014-02-16 146 views
27

所以我創建了一個分支關閉另一個分支,我已經創建,現在當我嘗試合併分支到主人,我遇到了必須合併兩個分支的情況。意外地分支了錯誤的分支,當我想合併到主人我必須合併兩個分支

這裏是一個圖

主 - >

->Branch 1 -> Branch 2 

我希望能夠合併只是在分支2到主站的更改,而無需如果合理,合併分支1上的更改。我看着重置和恢復,但似乎這些東西將刪除我所做的與分支2的所有更改。任何想法?

感謝

+0

的Git或SVN .... –

+1

假設你的意思'git',你想要'git rebase --onto'。 –

回答

8

一種方式做到這一點,則使用cherry-pick。做一個git log branch2並找到提交ID你想要的,然後切換到使用git checkout mastermaster分支然後用git cherry-pick <commit_id>

請參閱這篇文章瞭解更多詳情

How to merge a specific commit in Git

44

嘗試git的變基--onto具有以下語法:

要使BRANCH2的變化上主,而不包括BRANCH1的

git rebase --onto master branch1 branch2 

git help rebase相關的輸出:

 Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto. 
     First let’s assume your topic is based on branch next. For example, a feature developed in topic depends on some functionality which is found in next. 

       o---o---o---o---o master 
        \ 
        o---o---o---o---o next 
             \ 
             o---o---o topic 

     We want to make topic forked from branch master; for example, because the functionality on which topic depends was merged into the more stable 
     master branch. We want our tree to look like this: 

       o---o---o---o---o master 
        |   \ 
        |    o'--o'--o' topic 
        \ 
        o---o---o---o---o next 

     We can get this using the following command: 

      git rebase --onto master next topic 

牢記風險和重訂基期的陷阱,這裏提到: http://git-scm.com/book/en/Git-Branching-Rebasing

+6

太棒了 - 你剛剛救了我的培根。 – jsd

+1

順便說一句,這工作得很好,謝謝! –

+0

知府,謝謝! – mahatmanich

相關問題