Compare Commits Between Git Branches

George Brocklehurst

Working with a lot of git branches can be a bit of a headache. Graph visualisations can get tangled and confusing, especially when they include more than just the branches you care about. Sound familiar? You need git show-branch.

I have a feature branch called stock-information on a project that’s hosted on Heroku. I want to compare it to my main branch and to the main branch on my staging remote:

git show-branch stock-information staging/main main

The output can be a little confusing at first, but once you learn how to read it it’s a huge time saver:

! [stock-information] WIP: Link to data series
 ! [staging/main] Add a description to Stock
  ! [main] Display Stocks
---
+   [stock-information] WIP: Link to data series
+   [stock-information~1] Create DataSeries for Stocks.
++  [staging/main] Add a description to Stock
++  [staging/main~1] Import external Stock information
+++ [main] Display Stocks

The first three lines are column headings. They show the commit at the tip of each of the branches I specified, with a ! to indicate which column will represent this branch in the lines that follow.

After the --- come the commits. The + characters near the start of the lines indicate which of the branches this commit is present on.

For example, the first commit only has a + in the first column. This lines up with the ! for stock-information in the heading section. So, we know that this commit is on the stock-information branch but not staging/main or main.

The third commit (“Add a description to Stock”) has a + in each of the first two columns, which indicates it is present on the stock-information and staging/main.

The output will end with the last commit that is present on all of the specified branches, indicated by a + in each of the leading columns.

What’s next

If you found this useful, you might also enjoy:

About thoughtbot

We've been helping engineering teams deliver exceptional products for over 20 years. Our designers, developers, and product managers work closely with teams to solve your toughest software challenges through collaborative design and development. Learn more about us.