0 votes
1 view
in Devops and Agile by (29.1k points)

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...

1 Answer

0 votes
by (49.6k points)

To get a commit count for a revision:

git rev-list --count <revision>

To get the commit count across all branches:

git rev-list --all --count

For build identifiers, I don’t suggest this method, but if you must, it's probably best to use the count for the branch you're building against. This 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.

Refer: https://git-scm.com/docs/git-rev-list#Documentation/git-rev-list.txt---count

Welcome to Intellipaat Community. Get your technical queries answered by top developers !