Skip to content

Git: Find latest common ancestor of two branches

To find the latest common ancestor of two branches, which is required, for instance, when squashing your commits, instead of scrolling through you git log history you can use the git-merge-base1 tool.

An example is worth a 1000 words

git merge-base __CURRENT_BRANCH__ __ANCESTOR_BRANCH__

# for instance
git merge-base develop main 
  # will output the hash of the latest commit in common

Note

Make sure that both branches are up to date to avoid the apocalypse!


  1. Official git-merge documentation can be found here: https://git-scm.com/docs/git-merge-base 

Comments