262

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.

Community
  • 1
  • 1
aaronbauman
  • 3,549
  • 2
  • 24
  • 30
  • 1
    possible duplicate of [Number of commits on branch in git](http://stackoverflow.com/questions/10913892/number-of-commits-on-branch-in-git) – endrigoantonini Oct 28 '13 at 20:49

12 Answers12

495

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
Peter van der Does
  • 14,018
  • 4
  • 38
  • 42
  • 12
    none of these show the right number, for example master and branchname show the same number of commits. – botbot Jun 14 '13 at 00:10
  • Comments don;t really allow code, but this should show it does work. ==== $ git init ==== $ touch test.txt ==== $ git add . ==== $ git commit -a ==== $ git rev-list --count HEAD => 1 ==== $ git rev-list --count master => 1 ==== $ git checkout -b test ==== $ git rev-list --count test => 1 ==== $ git rev-list --count HEAD ^master => 0 ==== $ touch test2.txt ==== $ git add . ==== $ git commit -a ==== $ git rev-list --count master => 1 ==== $ git rev-list --count test => 2 ==== $ git rev-list --count HEAD ^master => 1 ==== – Peter van der Does Jun 14 '13 at 12:11
  • 1
    I agree with @botbot. These aren't really accurate. For example, try adding some merge commits or pull/rebase and notice the counts as depicted above start to become unreliable. – Wil Moore III Sep 06 '13 at 19:25
  • 2
    @wilmoore You mean that you get an extra count after you merge a branch? This is technically a commit, and so it's counted. but if you don't want to count these commits add --no-merges. I'll update the answer. – Peter van der Does Sep 07 '13 at 03:21
  • I suppose it makes sense depending on the intention; however, if the goal is to say, match what github might show when you do a compare or a pull-request, then this isn't what you want. I suppose making that more clear in the answer would be helpful since both are reasonable goals. – Wil Moore III Sep 07 '13 at 03:50
  • 2
    rev-list --count flag doesn't exist in git 1.7. Right now, the downvoted-to-hell suggestions below using `git log` are working better than any other suggestions. – aaronbauman Jan 21 '16 at 14:20
  • @aaronbauman I assume you mean [this one](https://stackoverflow.com/a/11657353/712526)? `wc` was the first tool I reached for, and it gave me the same result as this answer, for a simple history with no merges. – jpaugh Nov 08 '17 at 20:35
  • As said before, this solution does not actually work. The correct solution is given in the accepted answer in https://stackoverflow.com/questions/10913892/number-of-commits-on-branch-in-git – Alexander Amelkin Jun 28 '18 at 17:30
  • My problem: `git rev-list --count HEAD ^master fatal: ambiguous argument 'master': both revision and filename Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'`. Seems to work as `git rev-list --count HEAD ^master --`. – Akito May 26 '20 at 10:14
  • Does this also count stashes? Also commits on a branch that branched off the current branch? – Embedded_Mugs Jun 22 '21 at 17:44
  • git rev-list --count HEAD ^ this give me 0 which is not correct – Mr. Robot Aug 26 '22 at 08:29
72

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
Asnad Atta
  • 3,855
  • 1
  • 32
  • 49
53

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

scanny
  • 26,423
  • 5
  • 54
  • 80
  • 1
    Should be the same. The [docs says so](https://git-scm.com/docs/git-rev-list#_description). ```A special notation ".." can be used as a short-hand for "^'' ". For example, either of the following may be used interchangeably: $ git rev-list origin..HEAD $ git rev-list HEAD ^origin ``` – dosentmatter Feb 06 '20 at 02:09
  • I'm confused: `git fetch upstream; BEHIND=$(git rev-list --count HEAD..upstream/master); git merge --ff-only upstream/master~$BEHIND;` is not lining up. BEHIND is like 1800 when in reality nothing greater than merge upstream/master~400 produces changes. using `--no-merges` isn't much better, gives like 900. And if I do a merge like this with ~800, and the rev-list count is 1800, then I do a merge with ~790 I get between 6 and 28 lower count in rev-list. – dlamblin Feb 06 '20 at 02:42
19

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.

Community
  • 1
  • 1
kyb
  • 7,233
  • 5
  • 52
  • 105
  • git rev-list abc0923f.. --count --first-parent is giving proper results for my branch but first command is giving a big value – Jiss Raphel Aug 13 '18 at 08:29
5

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

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

Remear
  • 1,927
  • 12
  • 19
4

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

inorganik
  • 24,255
  • 17
  • 90
  • 114
3

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

Imran Ali
  • 2,223
  • 2
  • 28
  • 41
Paul Lan
  • 665
  • 2
  • 9
  • 12
  • works perfectly on local machine however not on CI, jenkins for instance will clone with depth 1 and then your commitCountOfCurrentBranch is always [0,1] – Kibotu Dec 30 '20 at 14:13
3

If you are using a UNIX system, you could do

git log|grep "Author"|wc -l
circa
  • 39
  • 1
2

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

git log <branch_name> --oneline | wc -l
kjw0188
  • 3,625
  • 16
  • 28
2

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.

sdoe
  • 21
  • 4
  • nice. it’s also possible to directly specify both branches, alleviating the need to switch branches: `master..current-development-branch` – Darklighter Nov 10 '20 at 18:28
0

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 }'
The Flash
  • 33
  • 5
-2

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

and get the result back

james
  • 191
  • 3
  • 9
  • 3
    This is not reliable. It would match commits that have "commit" in the commit message twice, for example. – rdb Jun 12 '17 at 10:41
  • 1
    @rdb No it won't. It will only output *number of lines* containing word "commit", so one line won't ever be counted twice. – iBug Mar 05 '18 at 09:45
  • @iBug: You're missing the point. If the commit message contains the word "commit", it appears on a separate line from the "commit a1b2c..." line in the `git log` output, so that commit will be counted twice in the result. Even worse if the commit message were to contain the word "commit" twice on two separate lines. – rdb Mar 06 '18 at 10:45
  • This works: `git log | grep "^commit" | wc -l`. Or even, `git log | grep -c "^commit"` – Self Evident Feb 14 '22 at 06:45