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...
git shortlog
is one way.To get a commit count for a revision (
HEAD
,master
, a commit hash):To get the commit count across all branches:
I recommend against using this for build identifier, but if you must, it's probably best to use the count for the branch you're building against. That way the same revision will always have the same number. If you use the count for all branches, activity on other branches could change the number.
Git shortlog is one way to get the commit details:
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.
This command returns count of commits grouped by committers:
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.
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?.