[git] Count the number of commits on a Git branch

I found this answer already: 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.

This question is related to git

The answer is


If you are using a UNIX system, you could do

git log|grep "Author"|wc -l

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?".


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.


One way to do it is list the log for your branch and count the lines.

git log <branch_name> --oneline | wc -l

I like doing git shortlog -s -n --all. Gives you a "leaderboard" style list of names and number of commits.


You can also do git log | grep commit | wc -l

and get the result back


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.


How about git log --pretty=oneline | wc -l

That should count all the commits from the perspective of your current branch.


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


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 }'