7

I am trying to count the number of commits since a tag was made.

I have tried using git rev-list but it seems to return the same results no matter what I try. This is what I have tried:

$ git rev-list 1.7Start^..HEAD | wc -l
13902
$ git rev-list HEAD | wc -l
13902

Trying to count how many commits since the 1.7Start tag was created. I'm currently on master hence using HEAD but using git rev-list master | wc -l gives me the same.

There hasn't been 13000+ commits since 1.7

Should git rev-list master show me every commit in master and hence yield a larger number than 1.7Start^..master which should just give me the difference?

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Nathan W
  • 54,475
  • 27
  • 99
  • 146
  • That suggests to me that there's no history in common between `1.7Start^` and `HEAD`. To test if that's the case, see what `git merge-base 1.7Start^ HEAD` produces - my guess is an error indicating that there's no commit in common. – Mark Longair May 31 '12 at 05:08
  • no error, and no return value – Nathan W May 31 '12 at 05:18
  • Right, that indicates that there is no history in common, which is surprising. How was this tag generated? – Mark Longair May 31 '12 at 05:22
  • Or, to be more precise, how is it that `1.7Start` has a different root commit from `HEAD`? – Mark Longair May 31 '12 at 05:32
  • 1
    ahh I see what has happened now. At around that same we time migrated from svn to git. The 1.7 branch was already created in svn and so the commits in the 1.7 branch differ from master because it never branched from there. I found the same commit in master and created a new tag. Works fine now. – Nathan W May 31 '12 at 05:38
  • git rev-list --count HEAD ^ See also: https://stackoverflow.com/questions/11657295/count-the-number-of-commits-on-a-git-branch – jose Dec 22 '20 at 12:42

3 Answers3

5

The results that you're getting suggest that there is no history in common between 1.7Start^ and HEAD, so 1.7Start and HEAD must have different root commits. (The syntax a..b when passed to git rev-list just means "every commit in b which isn't in a.)

In the comments above, the questioner indicated that this arose because the repository was migrated from Subversion, and master is entirely distinct from the imported branch that 1.7Start points to.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
4

If you only care about the last tag, as is typical, git describe will tell you what the last tag is and how many commits were made in the current branch since it. For example, in the output below, the last tag was 0.1.9 and 67 commits were made in the current branch since it.

$ git describe --tags

0.1.9-67-gff9fd30

For verification, you can see the full list of commits using the command below.

$ git log --oneline $(git describe --tags --abbrev=0)..

If you pipe its output to wc -l, the same count should be returned. Note that !! is the previous command in Bash.

$ !! | wc -l

67
Asclepius
  • 57,944
  • 17
  • 167
  • 143
  • Combining this with a build phase bash scripts for Xcode suggested here https://stackoverflow.com/a/24750392/84682 - the build number can automatically be updated to reflect the number of Git commits since last release (most recent tagged). Perfect. – Johan Jun 23 '18 at 07:41
3

Git has git rev-list --count which does this faster then wc-l.

There is also the git rev-list --use-bitmap-index --count in later versions of git, which is an optimization of --count.

rev-list needs a commit, so an example, to find all the commits in your repo for your current branch.

git rev-list --count HEAD 
lsiebert
  • 667
  • 1
  • 5
  • 16