How can I check whether two branches are “even”?

跟風遠走 提交于 2019-12-03 05:10:01

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

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 areeven 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

Compare the commit hashes

If you want to check whether two branches are exactly equivalent, just compare the commit hash:

-> git branch -v
  master                       0ccca51 Commencing the awesome
                               ^^^^^^^

-> git fetch
-> git branch -v -r
  origin/master                0ccca51 Commencing the awesome
                               ^^^^^^^
  origin/other                 0ccca51 Commencing the awesome
                               ^^^^^^^

If the commit hashes match - the branches are neither ahead nor behind one another, they are exactly the same.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!