Search code examples
gitbranchcommit

Using Git, find commit that is reachable from both branchA and branchB


I have two branches where I want to find the latest shared commits. Shared commits mean only those commits that are reachable from both branches, but unreachable from other commits reachable from both branches. How can I do this?


Solution

  • Here's the missing answer:

    git merge-base branchA branchB
    

    In case both branchA and branchB point to the same commit, the output will the that commit sha1, otherwise it will be sha1 of the closest common ancestor. Note that it's possible to give more than 2 branches at the same time, see git help merge-base for more details.