简体   繁体   中英

How do merge two branches with related changes into master?

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...

Whenever you are ready to merge branch2 , you should rebase it onto branch1 (if branch1 is not yet merge, or on top of master (if branch1 was already merged)

That way:

  • you resolve the conflicts locally, by replaying branch2 commits on top of master
  • the final merge will be a fast-forward (trivial) one.

That is, starting situation:

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

Once you are ready, b1 might have evolved and been merged to master:

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

A rebase --onto will be enough:

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)

Then a merge master will be trivial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM