How do I get the Git commit count?

2019-01-01 09:31发布

问题:

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:

To get a commit count for a revision (HEAD, master, a commit hash):

git rev-list --count <revision>

To get the commit count across all branches:

git rev-list --all --count

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.



回答2:

git shortlog is one way.



回答3:

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.



回答4:

This command returns count of commits grouped by committers:

git shortlog -s

git shortlog -s
14 John lennon
9  Janis Joplin


回答5:

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



回答6:

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

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.



回答7:

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

export GIT_REV_COUNT=`git rev-list --all --count`


回答8:

U can just use :

git shortlog -s -n

Result :

 827  user one
    15  user two
     2  Gest 


回答9:

A simple way is:

 git log --oneline | wc -l

oneline ensures that.



回答10:

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.



回答11:

git rev-parse --short HEAD



回答12:

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



回答13:

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.



回答14:

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\"

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.



回答15:

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)


回答16:

The one I used to use was:

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

Simple but it worked.



回答17:

Using Bash syntax,

$(git rev-list --count HEAD)

looks fine for purely linear history. If you also want to sometimes have “numbers” from branches (based off master), consider:

$(git rev-list --count $(git merge-base master HEAD)).$(git rev-list --count ^master HEAD)

When run from a checkout of master, you get simply 1234.0 or the like. When run from a checkout of a branch you will get something like 1234.13, if there have been 13 commits made on that branch. Obviously this is useful only insofar as you are basing at most one branch off a given master revision.

--first-parent could be added to the micro number to suppress some commits arising only from merging other branches, though it is probably unnecessary.



回答18:

You can try

git log --oneline | wc -l

or to list all the commits done by the people contributing in the repository

git shortlog -s


回答19:

git config --global alias.count \'rev-list --all --count\'

If you add this to your config, you can just reference the command;

git count



回答20:

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\"