26

I'm writing a small script and want to know how many commits I made on a current branch since it was created.

In this example I'd have 2 commits made on child:

git checkout master
git checkout -b child
...
git commit -a
...
git commit -a

So what I want is

commit_number = ...
echo $commit_number
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Andrey Ermakov
  • 3,298
  • 1
  • 25
  • 46
  • possible duplicate of [Number of commits in a git repository](http://stackoverflow.com/questions/1199312/number-of-commits-in-a-git-repository) – Zoltan Toth Jun 06 '12 at 12:09
  • 2
    @ZoltanToth answer from your question returns number of commits since repository creation, not since branch was created. – Andrey Ermakov Jun 06 '12 at 12:14
  • Please consider my answer (supposing better): https://stackoverflow.com/a/47133753/931908 – Paul Lan Nov 06 '17 at 10:55

5 Answers5

63

Git can provide you with the number of commits without further shell scripting.

git rev-list master.. --count

rev-list (listed in git help -a) is used to work with revisions.

As master.. will list the commits from the base of master and the current branch up to the current branch, --count will give you the count of them.

If you would instead want to have the number of commits between the two revisions you would use master.... To elaborate: between as in from master to the most recent common ancestor of master and the current branch (HEAD), and up to the current branch again. If you visualize the commit history as a tree you should be able to follow the two branches from the common ancestor. master.. on the other hand will just count one of the two branches.

So whether you want to use master.. or master... depends on whether you want to know how many commits you made in your branch since you split it off (master..), or the difference between the current master and branch, the number of commits in master and the branch since the branch was split off.

user1133275
  • 2,642
  • 27
  • 31
Kissaki
  • 8,810
  • 5
  • 40
  • 42
  • 3
    This is a better solution than the accepted answer, though I would put the option before the committishes: `git rev-list --count master..` – Nicholas Shanks Feb 17 '14 at 11:52
  • Better answer as it covers non *nix based platforms too. – gnuchu Mar 02 '16 at 09:49
  • 1
    For people who are interested in knowing what the detailed difference between double dots vs triple dots, please take a look at here https://stackoverflow.com/a/24186641/2305243. Really nice illustration. – Alan Dong Aug 23 '18 at 20:26
17

Assuming you branched from master, master..yourbranch gives you the range of commits that are in yourbranch but not in master.

Then you just have to list them one line each, and count the number of lines:

git log master..yourbranch --pretty=oneline | wc -l
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • This works well. You might replace `yourbranch` with `HEAD` *or* the empty string so the command just works for everyone without editing. – David Grayson Dec 16 '22 at 01:42
8

Update: git rev-list now has --count:

git rev-list --count master..

With older git versions:

git rev-list master.. |wc -l

rev-list lists revisions, and master.. refers to commits since current HEAD diverged from master.

user1338062
  • 11,939
  • 3
  • 73
  • 67
1

If you want to see how many commits you or anyone else has made, starting from your current HEAD, you can do:

git shortlog -sn

Example output:

   490  Donald Duck
   312  Some Developer
   274  John Doe
   144  Jane Doe
     5  Leet Hacker
Martin G
  • 17,357
  • 9
  • 82
  • 98
1

The following command will count the total branch commits, ignoring the merge commits.

  • git rev-list --count --no-merges master..

The rev-list option is used to work with the revision list.
The option --count will only return a number while --no-merges ignores the merges when two or more people work together and don’t update their local copy before they commit.

Please visit the following link to see the picture with explanation.

Ref: https://improveandrepeat.com/2017/10/little-git-tricks-how-many-commits-are-in-a-branch/

Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42