Search code examples
gitambiguityancestor

Can the most recent common ancestor in git be ambiguous?


Since revisions can have multiple parents (merges) and multiple children, can't the "most recent common ancestor" be ambiguous?

For example:

A---B--C--D                                                                                                                                               
 \   \   /
  \   E /
   \ / /
    F-G

The most recent common ancestor for E and D might be B or F.

I tried replicating this in git and I think it gave B as the common ancestor. Is there a rationale here to pick B over F? Perhaps resorting to timestamps? Or is it simply the first common ancestor discovered by the algorithm, which would be an arbitrary implementation detail?


Solution

  • It's not just possible, it's mentioned in the documentation for git merge-base:

    git merge-base finds best common ancestor(s) between two commits to use in a three-way merge. One common ancestor is better than another common ancestor if the latter is an ancestor of the former. A common ancestor that does not have any better common ancestor is a best common ancestor, i.e. a merge base. Note that there can be more than one merge base for a pair of commits.

    [emphasis mine—I think the emphasized sentence should include the word "best", really], and:

    ... Neither one is better than the other (both are best merge bases). When the --all option is not given, it is unspecified which best one is output.

    (which means, as you suggested, it's an implementation detail).