Search code examples
gitdiffthree-way-merge

Why does a merge need you to consider the common ancestor?


I'm learning Git, and am finding the tutorial on this site very useful. On that page, concerning merging, it includes:

         +---------- (D) ---------------+
        /             |                  \
(A) -- (B) -- (C) -------------- (E) -- (F)
                      |                  |
                 fix-headers           master
                                         |
                                        HEAD

The merge commit is (F), having parents (D) and (E). Because (B) is the common ancestor between (D) and (E), the files in (F) should contain the changes between (B) and (D), namely the heading fixes, incorporated into the files from (E).

I don't understand why you would worry about the common ancestor B. Why can't you just merge D and E to produce F? D will have been derived from B but might contain differences you want. There might have been a number of commits between B and D, with changes being made and taken out.


Solution

  • This is simply because merging works better if you know the common ancestor. See this answer, it has good explanation why.