Count the number of commits on a Git branch

git
131,729

Solution 1

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

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

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

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

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

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

Share:
131,729

Related videos on Youtube

aaronbauman
Author by

aaronbauman

I make websites.

Updated on July 08, 2022

Comments

  • aaronbauman
    aaronbauman 11 months

    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.

  • Hengjie
    Hengjie over 10 years
    Which column do you count? Is it the first one?
  • botbot
    botbot almost 10 years
    none of these show the right number, for example master and branchname show the same number of commits.
  • Peter van der Does
    Peter van der Does almost 10 years
    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 ====
  • Wil Moore III
    Wil Moore III over 9 years
    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.
  • Peter van der Does
    Peter van der Does over 9 years
    @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.
  • Wil Moore III
    Wil Moore III over 9 years
    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.
  • aaronbauman
    aaronbauman over 7 years
    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.
  • Ciasto piekarz
    Ciasto piekarz almost 7 years
    what are these numbers before names ? can you explain ?
  • Asnad Atta
    Asnad Atta almost 7 years
    @Ciastopiekarz these are number of commits by each person.
  • rdb
    rdb almost 6 years
    This is not reliable. It would match commits that have "commit" in the commit message twice, for example.
  • jpaugh
    jpaugh over 5 years
    @aaronbauman I assume you mean this one? 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.
  • iBug
    iBug about 5 years
    @rdb No it won't. It will only output number of lines containing word "commit", so one line won't ever be counted twice.
  • rdb
    rdb about 5 years
    @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.
  • Alexander Amelkin
    Alexander Amelkin almost 5 years
    As said before, this solution does not actually work. The correct solution is given in the accepted answer in stackoverflow.com/questions/10913892/…
  • Jiss Raphel
    Jiss Raphel almost 5 years
    git rev-list abc0923f.. --count --first-parent is giving proper results for my branch but first command is giving a big value
  • dosentmatter
    dosentmatter over 3 years
    Should be the same. The docs says so. A special notation "<commit1>..<commit2>" can be used as a short-hand for "^'<commit1>' <commit2>". For example, either of the following may be used interchangeably: $ git rev-list origin..HEAD $ git rev-list HEAD ^origin
  • dlamblin
    dlamblin over 3 years
    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.
  • Akito
    Akito about 3 years
    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 <command> [<revision>...] -- [<file>...]'. Seems to work as git rev-list --count HEAD ^master --.
  • Darklighter
    Darklighter over 2 years
    nice. it’s also possible to directly specify both branches, alleviating the need to switch branches: master..current-development-branch
  • Kibotu
    Kibotu over 2 years
    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]
  • Embedded_Mugs
    Embedded_Mugs almost 2 years
    Does this also count stashes? Also commits on a branch that branched off the current branch?
  • Self Evident
    Self Evident over 1 year
    This works: git log | grep "^commit" | wc -l. Or even, git log | grep -c "^commit"