Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
786 views
in Technique[技术] by (71.8m points)

git - Number of Commits between two Commitishes

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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Before I give you an answer, consider this commit graph:

        o -----------
       /             
... - A - o - o - o - B
                /
        o ----- o

Each o represents a commit, as do A and B (they're just letters to let us talk about specific commits). How many commits are there between commits A and B?

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 how git 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 commit B (while omitting commit A), 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 and B 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...