问题描述
如何在 git
?
中找到两次提交之间的提交数量另外,是否有一些我可以用GitHub上的任何项目(使用用户界面,而不是API)来做同样的事情吗?
你的答案,考虑这个提交图:
o -----------
$每个
/ \
... - A - o - o - o - B
\ /
o ----- o
o
代表一个提交,如A
和B
(它们只是让我们谈论特定提交的字母)。提交A
和B
?
git rev-list --count A..B ,然后决定between之间的含义(包括B和排除A?这就是git rev-list --count
将如何表现)。在这样的分支案例中,您将获得所有分支的所有提交;例如,添加- 第一个父母
,以便跟随主线。
提到我们可能已经注释了标签,这不会影响
git rev-list
的输出,它只计算特定的提交。)
编辑:由于
git rev-list --count A..B
includes提交B
(同时省略提交A
),并且您想排除两个端点,则需要减去一个。在现代shell中,你可以使用shell算法来做到这一点:
count = $(($(git rev-list --count A ... b) - 1))
例如:
$ x = $(($(git rev-list --count HEAD〜3..HEAD) - 1))
$ echo $ x
2
(这个特殊的回购有一个非常线性的图形结构,所以这里没有分支,在小费和小费后之间有两次提交)。但是,请注意,如果
A
和B
识别相同 commit:
$ x = $(($(git rev-list --count HEAD..HEAD) - 1))
$ echo $ x
-1
所以你可能想要检查第一:
count = $(git rev-list --count $ start .. $ end)
if [ $ count -eq 0];然后
...可能的错误:开始和结束是相同的提交...
else
count = $((count - 1))
fi
How can I find the number of commits between two commitishes in
git
?Additionally, is there some way that I could do the same with any project on GitHub (using the UI, not the API)?
解决方案Before I give you an answer, consider this commit graph:
o ----------- / \ ... - A - o - o - o - B \ / o ----- o
Each
o
represents a commit, as doA
andB
(they're just letters to let us talk about specific commits). How many commits are there between commitsA
andB
?That said, in more linear cases, just use
git rev-list --count A..B
and then decide what you mean by "between" (does it include B and exclude A? that's howgit rev-list --count
will behave). In branchy cases like this, you'll get all the commits down all the branches; add--first-parent
, for instance, to follow just the "main line".(You also mentioned "commitish", suggesting that we might have annotated tags. That won't affect the output from
git rev-list
, which only counts specific commits.)Edit: Since
git rev-list --count A..B
includes commitB
(while omitting commitA
), and you want to exclude both end-points, you need to subtract one. In modern shells you can do this with shell arithmetic:count=$(($(git rev-list --count A..B) - 1))
For instance:
$ x=$(($(git rev-list --count HEAD~3..HEAD) - 1)) $ echo $x 2
(this particular repo has a very linear graph structure, so there are no branches here and there are two commits "between" the tip and three-behind-the-tip). Note, however, that this will produce -1 if
A
andB
identify the same commit:$ x=$(($(git rev-list --count HEAD..HEAD) - 1)) $ echo $x -1
so you might want to check that first:
count=$(git rev-list --count $start..$end) if [ $count -eq 0 ]; then ... possible error: start and end are the same commit ... else count=$((count - 1)) fi
这篇关于两次提交之间的提交次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!