gitmerge-base

git merge-base not working on merged branch


I tried to get ancestor of my branch with git merge-base command on merged branch.

When I try git merge-base develop branch1 it shows sha YYY instead of XXX

* `develop`
|
* merge branch1 into develop
|\
| * `branch1` <- HEAD sha YYY
|/
* sha XXX

Everything works fine when I fire the same command on not merged branch (diagram bellow)

* `develop`
|
|
| * `branch1` <- HEAD sha YYY
|/
* sha XXX

The only way I get this commit id is by git log --oneline --boundary develop...branch1 It shows me a list of commits but I need only one.


Solution

  • That's because the merge base after the merge is YYY:

    * `develop`
    |
    * merge branch1 into develop
    |\
    | * `branch1` <- HEAD sha YYY
    |/
    * sha XXX
    

    is a vertical version of this same horizontal drawing:

    X---M--D   <-- develop
     \ /
      Y   <-- branch1
    

    The merge base of any two commits, such as Y and D, is the nearest commit reachable from both commits.

    Y reaches itself in zero steps; D reaches M and then Y in two steps; so Y is a nearby common ancestor.

    While X is also a common ancestor, it is clearly more distant: Y reaches X in one step, and D reaches X in two or three steps, either D-M-X or D-M-Y-X. So D-to-X is no further than D-to-Y (min path is 2 steps either way), but Y-to-Y is clearly shorter than Y-to-X.

    This means that Y is the best common ancestor, and hence is the merge base.