How do I get the Git commit count?

2019-01-01 09:22发布

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

20条回答
忆尘夕之涩
2楼-- · 2019-01-01 10:13

The one I used to use was:

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

Simple but it worked.

查看更多
梦寄多情
3楼-- · 2019-01-01 10:13

Use git shortlog just like this

git shortlog -sn

Or create an alias (for ZSH based terminal)

# show contributors by commits alias gcall="git shortlog -sn"

查看更多
步步皆殇っ
4楼-- · 2019-01-01 10:16

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

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

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

(As Bombe's answer points out, Git has actually an algorithm of its own, based on the latest tag, plus the number of commits, plus a bit of an SHA-1 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 generated during the packaging process, effectively linking an applicative build number to a technical revision id.

查看更多
心情的温度
5楼-- · 2019-01-01 10:17

U can just use :

git shortlog -s -n

Result :

 827  user one
    15  user two
     2  Gest 
查看更多
残风、尘缘若梦
6楼-- · 2019-01-01 10:17

A simple way is:

 git log --oneline | wc -l

oneline ensures that.

查看更多
君临天下
7楼-- · 2019-01-01 10:17

In our company, we moved from SVN to Git. Lack of revision numbers was a big problem!

Do git svn clone, and then tag the last SVN commit by its SVN revision number:

export hr=`git svn find-rev HEAD`
git tag "$hr" -f HEAD

Then you can get the revision number with help of

git describe --tags --long

This command gives something like:

7603-3-g7f4610d

Means: The last tag is 7603 - it's the SVN revision. 3 - is count of commits from it. We need to add them.

So, the revision number can be counted by this script:

expr $(git describe --tags --long | cut -d '-' -f 1) + $(git describe --tags --long | cut -d '-' -f 2)
查看更多
登录 后发表回答