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:
Branch
A
and branchB
are even.
is GitHub parlance for
Branch
A
and BranchB
point to the same commit.
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.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With