How to get the git commit count?

0 votes
asked Mar 24, 2009 by splo

I'd like to get the number of commits of my git repository, a bit like SVN revision numbers. The goal is to use it as a unique, incrementing build number.

I currently do like that, on Unix/Cygwin/msysGit:

git log --pretty=format:'' | wc -l

But I feel it's a bit of a hack.

Is there a better way to do that? It would be cool if I actually didn't need wc or even git, so it could work on a bare Windows. Just read a file or a directory structure ...

15 Answers

0 votes
answered Jan 24, 2009 by vonc

You are not the first one to think about a "revision number" in Git, but 'wc' is quite dangerous, since commit can be erased or squashed, and the history revisited.

The "revision number" was especially important for Subversion since it was needed in case of merge (SVN1.5 and 1.6 have improved on that front).

What you could end up with is a pre-commit hook which would include in the comment a revision number, with an algo not involving looking up the all history of a branch to determine the correct number.

Bazaar actually came up with such an algo and it may be a good starting point for what you want to do.

(As Bombe's answer points out, Git has actually an algo of its own, based on the latest tag, plus the number of commits, plus a bit of SHA1 key). You should see (and upvote) his answer if it works for you.


To illustrate Aaron's idea, you can also append the git commit hash into an application’s "info" file you are distributing with your application.

That way, the about box would look like:

about box

The applicative number is part of the commit, but the 'application’s "info" file' is generating during the packaging process, effectively linking an applicative build number to a technical revision id.

0 votes
answered Jan 24, 2009 by aaron-digulla

Generate a number during the build and write it to a file. Whenever you make a release, commit that file with the comment "Build 147" (or whatever the build number currently is). Don't commit the file during normal development. This way, you can easily map between build numbers and versions in Git.

0 votes
answered Jan 24, 2009 by bombe

If you’re looking for a unique and still quite readable identifier for commits, git describe might be just the thing for you.

0 votes
answered Jan 24, 2009 by pat-notz

There's a nice helper script that the Git folks use to help generate a useful version number based on Git describe. I show the script and explain it in my answer to How would you include the current commit id in a git project’s files? Hope that helps.

0 votes
answered Jan 14, 2011 by pltvs

This command returns count of commits grouped by commiters:

git shortlog -s
git shortlog -s
14 John lennon
9  Janis Joplin
0 votes
answered Jan 15, 2011 by john-gietzen

To get it into a variable, the easiest way is:

export GIT_REV_COUNT=`git rev-list --all --count`
0 votes
answered Jan 31, 2011 by robert-massaioli

The one I used to use was:

git log | grep "^commit" | wc -l

Simple but it worked.

0 votes
answered Jan 25, 2012 by murthy-upadhyayula

Git shortlog is one way to get the commit details:

git shortlog -s -n

This will give the number of commits followed by the author name. The -s option removes all the commit messages for each commit that the author made. Remove the same option if you would like to see the commit messages also. The -n option is used for sorting the entire list. Hope this helps.

0 votes
answered Jan 26, 2012 by nuclearpeon

If you're just using one branch, such as master, I think this would work great.

git rev-list --full-history --all | wc -l

This will only output a number. You can alias it to something like

git revno

to make things really convenient. To do so, edit your .git/config file and add this in:

[alias]
    revno = "!git rev-list --full-history --all | wc -l"

EDIT: This will not work on Windows. I do not know the equivalent of "wc" for that OS, but writing a python script to do the counting for you would be a multi-platform solution.

0 votes
answered Jan 9, 2013 by jake-berger

git rev-list HEAD --count

git rev-list

git rev-list <commit> : List commits that are reachable by following the parent links from the given commit (in this case, HEAD).

--count : Print a number stating how many commits would have been listed, and suppress all other output.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...