Count the number of commits on a Git branch
GitGit Problem Overview
I found this answer already: https://stackoverflow.com/questions/10913892/number-of-commits-on-branch-in-git but that assumes that the branch was created from master.
How can I count the number of commits along a branch without relying on that assumption?
In SVN this is trivial, but for some reason is really difficult to figure out in git.
Git Solutions
Solution 1 - Git
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
Solution 2 - Git
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
Solution 3 - Git
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?".
Solution 4 - Git
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.
Solution 5 - Git
How about git log --pretty=oneline | wc -l
That should count all the commits from the perspective of your current branch.
Solution 6 - Git
I like doing git shortlog -s -n --all
. Gives you a "leaderboard" style list of names and number of commits.
Solution 7 - Git
If you are using a UNIX system, you could do
git log|grep "Author"|wc -l
Solution 8 - Git
One way to do it is list the log for your branch and count the lines.
git log <branch_name> --oneline | wc -l
Solution 9 - Git
Well, the selected answer doesn't work if you forked your branch out of unspecific branch (i.e., not master
or develop
).
Here I offer a another way I am using in my pre-push
git hooks.
# Run production build before push
echo "[INFO] run .git/hooks/pre-push"
echo "[INFO] Check if only one commit"
# file .git/hooks/pre-push
currentBranch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
gitLog=$(git log --graph --abbrev-commit --decorate --first-parent HEAD)
commitCountOfCurrentBranch=0
startCountCommit=""
baseBranch=""
while read -r line; do
# if git log line started with something like "* commit aaface7 (origin/BRANCH_NAME)" or "commit ae4f131 (HEAD -> BRANCH_NAME)"
# that means it's on our branch BRANCH_NAME
matchedCommitSubstring="$( [[ $line =~ \*[[:space:]]commit[[:space:]].*\((.*)\) ]] && echo ${BASH_REMATCH[1]} )"
if [[ ! -z ${matchedCommitSubstring} ]];then
if [[ $line =~ $currentBranch ]];then
startCountCommit="true"
else
startCountCommit=""
if [[ -z ${baseBranch} ]];then
baseBranch=$( [[ ${matchedCommitSubstring} =~ (.*)\, ]] && echo ${BASH_REMATCH[1]} || echo ${matchedCommitSubstring} )
fi
fi
fi
if [[ ! -z ${startCountCommit} && $line =~ ^\*[[:space:]]commit[[:space:]] ]];then
((commitCountOfCurrentBranch++))
fi
done <<< "$gitLog"
if [[ -z ${baseBranch} ]];then
baseBranch="origin/master"
else
baseBranch=$( [[ ${baseBranch} =~ ^(.*)\, ]] && echo ${BASH_REMATCH[1]} || echo ${baseBranch} )
fi
echo "[INFO] Current commit count of the branch ${currentBranch}: ${commitCountOfCurrentBranch}"
if [[ ${commitCountOfCurrentBranch} -gt 1 ]];then
echo "[ERROR] Only a commit per branch is allowed. Try run 'git rebase -i ${baseBranch}'"
exit 1
fi
For more analysis, please visit my blog
Solution 10 - Git
As the OP references Number of commits on branch in git I want to add that the given answers there also work with any other branch, at least since git version 2.17.1 (and seemingly more reliably than the answer by Peter van der Does):
working correctly:
git checkout current-development-branch
git rev-list --no-merges --count master..
62
git checkout -b testbranch_2
git rev-list --no-merges --count current-development-branch..
0
The last command gives zero commits as expected since I just created the branch. The command before gives me the real number of commits on my development-branch minus the merge-commit(s)
not working correctly:
git checkout current-development-branch
git rev-list --no-merges --count HEAD
361
git checkout -b testbranch_1
git rev-list --no-merges --count HEAD
361
In both cases I get the number of all commits in the development branch and master from which the branches (indirectly) descend.
Solution 11 - Git
You can use this command which uses awk on git bash/unix to get the number of commits.
git shortlog -s -n | awk '/Author/ { print $1 }'
Solution 12 - Git
You can also do git log | grep commit | wc -l
and get the result back