-1

I am trying to figure out if there is a way to get the count of the number of commits done on a specific branch.

I have tried using rev-list, but the count I am getting is different.

PS C:\Dev\code\TestProj> git checkout master
Already on 'master'                            
Your branch is up to date with 'origin/master'.
PS C:\Dev\code\TestProj> git checkout -B "TESTBRANCH"
Switched to a new branch 'TESTBRANCH'
PS C:\Dev\code\TestProj> git commit -a -m "TESTBRANCH-TEST COMMIT"
[TESTBRANCH 3a98967] TESTBRANCH-TEST COMMIT
 1 file changed, 1 insertion(+)            
PS C:\Dev\code\TestProj> git rev-list --count --first-parent TESTBRANCH
9
PS C:\Dev\code\TestProj> 

In the above code, I have made only one commit on the new branch which I created and I can see the count returned is 9. I think Git is taking into consideration some other revisions as well.

Is there a way to get the commit count as just 1?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Philemon philip Kunjumon
  • 1,417
  • 1
  • 15
  • 34
  • 2
    How specific do you need? Git doesn't keep a proper history of commits on a branch, since branches are temporary and change a lot – evolutionxbox Dec 08 '21 at 16:45
  • Your `git rev-list` exploration will by default go back to the initial commit. If you want to limit ancestry, you'll have to tell git that (for example, with [`--not`](https://git-scm.com/docs/git-rev-list#Documentation/git-rev-list.txt---not)). – Romain Valeri Dec 08 '21 at 16:46
  • @evolutionxbox even if its not for the count, i need to atleast get if the branch has the commits. having said that if i create a new branch and without any commits check the branch to see if there is any commits then it should return false . Hope this clarifies.. – Philemon philip Kunjumon Dec 08 '21 at 16:48
  • If you create a new branch on a commit, and that commit as ancestors, the count will always be greater than 1 – evolutionxbox Dec 08 '21 at 16:52
  • Where does the branch start? – Liam Dec 08 '21 at 16:52

2 Answers2

2

In this case, you'll want to use a commit range from master to TESTBRANCH. This will count all commits made on TESTBRANCH since it diverged from master:

git rev-list --count --first-parent master..TESTBRANCH

The answer to this should be 1.

If you pull later, and there are new commits on master, you can also count the number of commits since TESTBRANCH was created just by flipping the order of the arguments in the range:

git rev-list --count --first-parent TESTBRANCH..master

This syntax, of <branch A>..<branch B>, is called a commit range. For a more in-depth explanation, see this article on commit ranges, although a short summary would be:

<branch A>..<branch B> refers to all the commits on branch B, that aren't on branch A. So master..TESTBRANCH means "all the commits on TESTBRANCH, that aren't on master".

Alecto Irene Perez
  • 10,321
  • 23
  • 46
1

Your rev-list will just walk all the way to the very first commit. If TESTBRANCH is branched from main, you can try:

git log --oneline TESTBRANCH ^main
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
vaizki
  • 1,678
  • 1
  • 9
  • 12