Compare branches with git show-branch

Using git show-branch, you can compare commits and see how and where they differ from each other. For example:

# Clone a popular Javascript package repo as an example
git clone git@github.com:axios/axios.git
cd axios
git show-branch e518c5d e518c5d~5

This will yield output very similar to:

! [e518c5d] update security.md (#4784)
 ! [e518c5d~5] Bump eventsource from 1.1.0 to 1.1.1 (#4764)
--
+  [e518c5d] update security.md (#4784)
+  [e518c5d^] fixed(ci): removed wrkflow permissions that are wrong
+  [e518c5d~2^2] Update base with master (#4756)
+  [e518c5d~3] chore(GitHub Actions): rename master to main, added protections
+  [e518c5d~4] chore: Set permissions for GitHub actions (#4765)
++ [e518c5d~5] Bump eventsource from 1.1.0 to 1.1.1 (#4764)

It may not be immediately obvious, but this is a tabular layout. The top portion represents the table headers, with the ! characters acting as arrows pointing down the columns beneath:

! [e518c5d] update security.md (#4784)
 ! [e518c5d~5] Bump eventsource from 1.1.0 to 1.1.1 (#4764)
--
...

The remaining lines include a + in each column where the commit is present. So in this example it is clear that both branches have the commit:

...
++ [e518c5d~5] Bump eventsource from 1.1.0 to 1.1.1 (#4764)

After that, they diverge.

Why not use 'git diff' for this?
You can use git diff to see how file contents differ between two commits. However, git diff will not show you the difference in commits between two branches, i.e. where they share a common commit and where they split. That’s what git show-branch is for.

Why is this useful?

The example above is rather trivial (since it shows a fast-forward merge situation), but this technique becomes very powerful when you realize you can compare any two commits. Typically it’s useful to compare branches. For example if you’re working on a team, collaborating on a branch called feature/mega-feature, you can see if your colleagues have added any commits to the remote that you don’t yet have in your local copy of the branch:

git fetch origin
git show-branch HEAD origin/feature/mega-feature

At that point, if there are some new commits you don’t have yet, you can see exactly what they are. (Hopefully your colleagues are creating atomic commits with clear and concise commit messages). After seeing the commit messages, you might decide to run the following update your local branch by adding their commits first before your novel commit(s) that are not pushed yet:

git rebase origin/feature/mega-feature