8

To find the number of commits on a git branch you can do:

$ git rev-list --count HEAD
920

However, if you initially clone with --depth=1, that doesn't work:

$ git clone https://github.com/ndmitchell/hoogle.git --depth=1
$ cd hoogle
$ git rev-list --count HEAD
1

Is there any way to get the speed and reduced network traffic of a --depth=1 clone, but then also get the count of the number of commits?

Neil Mitchell
  • 9,090
  • 1
  • 27
  • 85

2 Answers2

4

Is there any way to get the speed and reduced network traffic of a --depth=1 clone, but then also get the count of the number of commits?

I'm pretty sure you can't.

As you know, --depth=1 only retrieves the most recently pushed commit. That means when you clone with a depth of 1 you get 1 commit and only that single commit, with no history at all attached to it.

As far as your local repository is concerned, there is no history, just this 1 commit.

As is also mentioned in the docs

--depth

Create a shallow clone with a history truncated to the specified number of revisions.

What I also find interesting that even if you'd check the origin

$ git rev-list --count origin/master
$ git log origin/master

they'd both only show 1 commit, too.

Tim
  • 41,901
  • 18
  • 127
  • 145
0

This may not be appropriate for all situations, but in certain use cases you may be able to convert your shallow clone to a full clone with (assuming Git 1.8.3+) as per How to convert a Git shallow clone to a full clone:

git fetch --unshallow

This will allow you to get the correct count as normal.

Loren Segal
  • 3,251
  • 1
  • 28
  • 29