Count the number of commits on a Git branch
To count the commits for the branch you are on:
git rev-list --count HEAD
for a branch
git rev-list --count <branch-name>
If you want to count the commits on a branch that are made since you created the branch
git rev-list --count HEAD ^<branch-name>
This will count all commits ever made that are not on the branch-name as well.
Examples
git checkout master
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^master
Result: 3
If your branch comes of a branch called develop
:
git checkout develop
git checkout -b test
<We do 3 commits>
git rev-list --count HEAD ^develop
Result: 3
Ignoring Merges
If you merge another branch into the current branch without fast forward and you do the above, the merge is also counted. This is because for git a merge is a commit.
If you don't want to count these commits add --no-merges
:
git rev-list --no-merges --count HEAD ^develop
How much commits was done to current branch since begin of history, not counting commits from merged branches:
git rev-list HEAD --count --first-parent
From documentation git rev-list --help:
--first-parent
Follow only the first parent commit upon seeing a merge commit. This option can give a better overview when viewing the evolution of a particular topic branch, because merges into a topic branch tend to be only about adjusting to updated upstream from time to time, and this option allows you to ignore the individual commits brought in to your history by such a merge. Cannot be combined with --bisect.
Note: Shallow clone will shrink the history size. E.g. if you clone with --depth 1
, will return 1.
number of commits done since some other commit:
git rev-list HEAD abc0923f --count --first-parent
or the same:
git rev-list abc0923f.. --count --first-parent
or use any other git reference:
git rev-list master tag-v20 --count --first-parent
Count commits done since 2018 year
git rev-list HEAD --count --first-parent --since=2018-01-01
01-01-2018, 01.01.2018, 2018.01.01 also works.
git rev-label
I wrote a script to get version-revision from Git in format like '$refname-c$count-g$short$_dirty'
which expands to master-c137-gabd32ef
.
Help is included to script itself.
To see total no of commits you can do as Peter suggested above
git rev-list --count HEAD
And if you want to see number of commits made by each person try this line
git shortlog -s -n
will generate output like this
135 Tom Preston-Werner
15 Jack Danger Canty
10 Chris Van Pelt
7 Mark Reid
6 remi
It might require a relatively recent version of Git, but this works well for me:
git rev-list --count develop..HEAD
This gives me an exact count of commits in the current branch having its base on master.
The command in Peter's answer, git rev-list --count HEAD ^develop
includes many more commits, 678 vs 97 on my current project.
My commit history is linear on this branch, so YMMV, but it gives me the exact answer I wanted, which is "How many commits have I added so far on this feature branch?".