How do merge two branches with related changes into master?

时间:2019-05-31 11:36:47

标签: git git-branch branching-and-merging

In GIT I have 2 BRANCHES and a MASTER.

BRANCH1 is checked out from master and it have some commits to be merged with master and merging of BRANCH1 will take some time because of some testing reasons.

In that time I've to work on another Branch which is BRANCH2. So BRANCH2 should have the commits of BRANCH1 as I'll do some changes upon BRANCH1's commits, then BRANCH2 will also be merged with MASTER after sometime when BRANCH1 merged with the master (BRANCH1 will be merged first).

  1. checkout from BRANCH1 then do some commits then merge it master after BRANCH1 merged for sometime.
  2. (OR) checkout from MASTER then rebase it with BRANCH1 then do some commits then merge it MASTER.

So What is the best option I can do for working on BRANCH2 with BRANCH1 change then merging it with MASTER?

Any suggestions are welcome and Thanks...

1 个答案:

答案 0 :(得分:1)

无论何时准备合并branch2,都应将其重新设置到branch1上(如果branch1尚未合并,或在master上(如果{{ 1}}已被合并)

那样:

  • 您可以通过在branch1顶部重播branch2提交来在本地解决冲突
  • 最后的合并将是一个快速(微不足道)的合并。

也就是说,开始情况:

master

一旦准备就绪,b1可能已经进化并合并到母版中:

m--m--m
       \
        b1 --b1--b1--b1
              \
               b2--b2

一个rebase --onto就足够了:

m--m--m--m--m---------------M--m--m (master)
       \                   /
        b1 --b1--b1--b1--b1         (branch1)
              \
               b2--b2--b2--b2       (branch2)

那么git rebase --onto master $(git merge-base branch1 branch2) branch2 m--m--m--m--m---------------M--m--m--b2'--b2'--b2'--b2' (branch2) \ / (master) b1 --b1--b1--b1--b1 (branch1) 会很简单