How can I check whether two branches are "even"?

To compare the files of a branch called branch located on GitHub against master on the same repository, you can use git diff:

git diff origin/branch origin/master

The following will work to compare the commits between the two branches:

git log --left-right --graph --cherry-pick --oneline origin/branch...origin/master

As mentioned in my comment above, it is possible that two branches can have identical files but different commits.


GitHub terminology

Branch A and branch B are even.

is GitHub parlance for

Branch A and Branch B point to the same commit.

Are the two branches even?

If you're only interested in whether the two branches are even or not, without any additional details (e.g. commit count), a script-friendly way is to simply test the SHAs of their tips for equality:

[ "$(git rev-parse <refA>)" = "$(git rev-parse <refB>)" ]

After running this command, the value of $? is 0 if <ref1> and <ref2> are even, and 1 otherwise. Because this command only involves the plumbing Git command git-rev-parse, it can safely be used programmatically.

Is one branch ahead of or behind the other branch?

If you want to emulate GitHub's functionality, e.g. print

foo is n commits ahead of bar

etc., you can use the following script:

#!/bin/sh

# git-checkeven.sh
#
# Check whether two revisions are even, and, otherwise, to what extent
# the first revision is ahead of / behind the second revision
#
# Usage: git checkeven <revA> <revB>
#
# To make a Git alias called 'checkeven' out of this script,
# put the latter on your search path, and run
#
#   git config --global alias.checkeven '!sh git-checkeven.sh'

if [ $# -ne 2 ]; then
    printf "usage: git checkeven <revA> <revB>\n\n"
    exit 2
fi

revA=$1
revB=$2

if ! git merge-base --is-ancestor "$revA" "$revB" && \
  ! git merge-base --is-ancestor "$revB" "$revA"
then
  printf "$revA and $revB have diverged\n"
  exit 1
fi

nA2B="$(git rev-list --count $revA..$revB)"
nB2A="$(git rev-list --count $revB..$revA)"

if [ "$nA2B" -eq 0 -a "$nB2A" -eq 0 ]
then
  printf "$revA is even with $revB\n"
  exit 0
elif [ "$nA2B" -gt 0 ]; then
  printf "$revA is $nA2B commits behind $revB\n"
  exit 1
else
  printf "$revA is $nB2A commits ahead of $revB\n"
  exit 1
fi

Test

Assume a toy repo with the following history:

         * [develop]
        /
... -- * [main, master]
        \
         * [release]

Then, after defining a Git alias called checkeven that calls the script above, you get...

$ git checkeven main develop
main is 1 commits behind develop
$ git checkeven develop main
develop is 1 commits ahead of main
$ git checkeven master main
master is even with main
$ git checkeven develop release
develop and release have diverged