Today I learned

Use git merge-base for big merges

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

You have just read Use git merge-base for big merges, written on December 10, 2015. This is Today I Learned, a collection of random tidbits I've learned through my day-to-day web development work. I'm Rico Sta. Cruz, @rstacruz on GitHub (and Twitter!).

← More articles