When trying to merge two Git branches that have diverged a lot, it’s a little difficult to make sense of what happened on both sides.

Enter git merge-base–a simple built-in utility to tell you where two branches diverged. It will tell you the commit where the two branches split off:

$ git merge-base develop master

Set up git tags

This command will create a tag _base that will point to where they both diverged.

git tag _base `git merge-base develop master`

Inspecting changes

You can then use git diff, git log, or tig to inspect changes on either side:

# Inspect changes in Gemfile on each side
tig _base...develop -- Gemfile
tig _base...master -- Gemfile

git diff _base...develop -- Gemfile
git diff _base...master -- Gemfile

Check if both branches diverged

You can also use the --independent flag which will show commits that can’t be reached by any other. If it prints 2 commits, it means that there are changes on both sides.

$ git merge-base develop master --independent

If it only prints 1, it means only one side has changes.

$ git merge-base develop master --independent