1

Here is my situation:

  • I have two feature branches A & B.

  • I want to combine these two branches.

I did the following:

  • Create a new branch C branching off from A

  • Checkout the new branch C

  • run git merge B from command line

I expect both the branches combined. If there are conflicting changes to the same files the command should raise a merge conflict.

But in actual, there is a same file which has different contents in branches A and B but there was no merge conflict reported. Instead, the contents of the file from branch A are overwritten by the contents of the file from branch B.

I have no clue why there was not a merge conflict reported and am more worried why the contents of the file was overwritten.

Any hints or pointers would help. Thanks.

ndim
  • 35,870
  • 12
  • 47
  • 57
blogs4t
  • 2,329
  • 5
  • 20
  • 33

1 Answers1

1

Let's lay out what happened.

  • I have two feature branches A & B.

This is usually something like this.

           4 - 5 [A]
          /
 1 - 2 - 3 - 6 [master]
              \
               7 - 8 [B]

A and B are branched off master. They diverge.

But you may have had this.

                 6 - 7 [B]
                /
           4 - 5 [A]
          /
 1 - 2 - 3 - 6 [master]

Here B is branched from A. B shares all of A's commits. Let's assume that.

  • Create a new branch C branching off from A

Creating a new branch in Git just makes a new label at the same commit as A.

                 6 - 7 [B]
                /
           4 - 5 [A][C]
          /
 1 - 2 - 3 - 6 [master]
  • Checkout the new branch C
  • run git merge B from command line

When this happens Git notices that C is an ancestor of B. So it does a "fast-forward" meaning it doesn't bother with a merge, it just advances C to where B is.

                 6 - 7 [B][C]
                /
           4 - 5 [A]
          /
 1 - 2 - 3 - 6 [master]

If A and B changed the same files it would appear that B overwrote the changes in A.

git log and Github unfortunately flatten out your Git history giving you a false impression that it's linear. You can see the true structure of your repository with git log --graph --decorate --all and check if it matches the scenario here.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • in my case, I could see both branches A and B are branched off from master (may not be at the same time though). If so, should i create my branch C off from master - then merge A into C and - then merge B into C? in order to have no missing commits from both A and B? – blogs4t Mar 14 '19 at 14:08