arunmmanoharan
arunmmanoharan

Reputation: 2675

Child Branch - Child Branch - Master - Git

Hi I have a master branch and I made a branch out of it(lets call it Branch A). I needed to implement a different feature off that branch and I created a branch out of branch A(lets say Branch B). Now Branch A is merged to master. How do I merge Branch B to master?

                         ------------------
                        /  BRANCH B
               ---------------------------
              /     BRANCH A
-----------------------------------------
       MASTER

Now it looks like this:

                         ------------------
                        /  BRANCH B
               ---------------------------
              /     BRANCH A
-----------------------------------------
       MASTER + BRANCH A CHANGES

Upvotes: 0

Views: 906

Answers (3)

Marina Liu
Marina Liu

Reputation: 38106

There are two ways you can merge B into master (assume A is merged into master at commit K):

A---B---C---D--------K  master
     \              /
       E---F---G---H    Branch A
            \
             I---G      Branch B

Option 1: rebase B onto A, and then merge B into master

git rebase --onto A A B, then the branch structure will look like:

A---B---C---D--------K        master
     \              /
       E---F---G---H          Branch A
                    \
                     I'---G'  Branch B

Then merge B into master (git merge B):

A---B---C---D-------K---------L  master
     \             /         /
      E---F---G---H         /    Branch A
                   \       /
                    I'---G'      Branch B

Option 2: merge B into master directly (this is will cause more conflicts)

git merge B

A---B---C---D--------K---L   master
     \              /   /
      E---F---G---H    /     Branch A
           \          /
            I--------G       Branch B

Upvotes: 0

Mark Adelsberger
Mark Adelsberger

Reputation: 45649

Probably you can do it the same way as any other merge.

I think people get a little too hung up on branch identity - or, more to the point, people conflate a "branch" (a movable ref; i.e. a name that points to some commit) with a set of commits. Taking your example

x --- O --- x --- x <--(master)
       \
        A1 --- A2 --- A3 <--(branchA)
                 \
                  B1 --- B2 <--(branchB)

Now branchA is a pointer that happens to currently point to A3. Many people think that branchA is the set of commits (A1, A2, A3) and so they reason incorrectly about what it means to delete the branch, or merge it, or rebase it, or operate on it in any way.

When you merged branchA to master, git didn't care about anything like that. It just saw that O was the closest commit reachable from both branchA and master; that A1, A2, and A3 were reachable from branchA but not from O; and maybe that various x commits were reachable from master but not from O. It used that information to calculate the merge (and asked you for help if it got into any difficulty doing so), then left you with

x --- O --- x --- x ----- M <--(master)
       \                 /
        A1 --- A2 --- A3 <--(branchA)
                 \
                  B1 --- B2 <--(branchB)

So now if you merge branchB git will probably identify A2 as the most recent commit reachable from both branchB and master; B1 and B2 as commits reachable from branchB but not from A2; and maybe M, A3, and various x commits as reachable from master but not from A2. And it's likely to calculate the resulting merge just fine.

That said, if it doesn't go smoothly, you could simplify things for git by checking out branchA (or A3, which is maybe tricky but still doable if you've deleted branchA), merging branchB, then checking out master and again merging branchA. This breaks the merge calculation into two smaller/simpler parts, and results in a merge topology which (depending on your preferences) is either more symmetric and therefore neater, or more cluttered with merges and therefore uglier.

Upvotes: 1

YoannFleuryDev
YoannFleuryDev

Reputation: 941

You just need to merge B like you merge A. Git will now how to handle the already merged commits from A.

git checkout master
git merge B

Upvotes: 1

Related Questions