Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

The GitHub web interface has a nice feature telling me whether a branch is even with the master branch.

Is there a command-line equivalent of this feature? I work with multiple repositories and I'm looking for a quick way to see whether branches are even or require attention.

Here are screenshot of the GitHub web interface, for those wondering about this feature:

enter image description here

enter image description here

like image 945
Nathan H Avatar asked Aug 13 '15 08:08

Nathan H


2 Answers

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

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:

... -- * [release71]
        \
         * [master, develop]

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

$ git checkeven release71 develop
release71 is 1 commits behind develop
$ git checkeven develop release71
develop is 1 commits ahead of release71
$ git checkeven master develop
master is even with develop
like image 152
jub0bs Avatar answered Oct 29 '22 11:10

jub0bs


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.

like image 23
Tim Biegeleisen Avatar answered Oct 29 '22 12:10

Tim Biegeleisen