问题
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:
回答1:
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.
回答2:
GitHub terminology
Branch
A
and branchB
are even.
is GitHub parlance for
Branch
A
and BranchB
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
回答3:
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.
来源:https://stackoverflow.com/questions/31982954/how-can-i-check-whether-two-branches-are-even