Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of Commits between two Commitishes

Tags:

git

github

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

like image 675
haneefmubarak Avatar asked Aug 13 '15 20:08

haneefmubarak


People also ask

How do I commit between two branches?

In some cases, you may be interested in knowing the commit differences between two branches. In order to see the commit differences between two branches, use the “git log” command and specify the branches that you want to compare.

How many commits can git handle?

1 accepted. Hi @Diliup-G , We don't have a limit on the number of commits per repository.

How can I see commits from one branch?

Git rev-list will list commits in one branch that are not in another branch. It is a great tool when you're trying to figure out if code has been merged into a branch or not. Using the --oneline option will display the title of each commit. The ^ operator excludes commits in the specified branch from the list.


2 Answers

$ git log 375a1..58b20 --pretty=oneline | wc -l 

Specify your start commit followed by your end commit, and then count the lines. That should be the count of commits between those two commit ranges. Use the --pretty=oneline formatting so that each commit takes up a single line.

Note that using two dots (375a1..58b20) is different than using three dots (375a1...58b20); see What are the differences between double-dot “..” and triple-dot “…” in Git commit ranges? for more information about this and to figure out which one you want to use.

As for the GUI in GitHub, I don't know of a way to accomplish this same task. But that should be trivial, as the above is the possible way to do it directly using Git and Bash.

like image 22
Thomas Stringer Avatar answered Sep 20 '22 10:09

Thomas Stringer


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 
like image 70
torek Avatar answered Sep 17 '22 10:09

torek