How do I get the Git commit count?
Asked Answered
S

26

1015

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

Sterilize answered 24/3, 2009 at 13:38 Comment(5)
You may find interesting answers here: what is the git equivalent for revision number?Diba
git rev-list HEAD --count git rev-listEntire
@jberger: I think your comment should be converted to an answer.Peres
@utapyngo: given the 13 other answers, I knew it'd be buried. I've posted it here then.Entire
@jberger, this answer doen't work for git1.7.0.Facsimile
S
1556

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 --count --all

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.

Saberio answered 24/3, 2009 at 13:39 Comment(18)
git shortlog | grep -E '^[ ]+\w+' | wc -l if you want to get total number and git shortlog | grep -E '^[^ ]' if you want to get commits number for every contributor.Fairminded
Thanks for pointing out wc -l. Minimalism FTW. I incorporated it into my answer.Saberio
Thanks this helped me a lot. However, there are a lot of answers here which I can clarify (as no-one else has). To return the total number of commits for your current branch: git shortlog | grep -E '^[ ]+\w+' | wc -l To return the total number of commits for all your branches: git rev-list --all | wc -lViddah
This solution is both hacky (similar to the git log --pretty=format:'' | wc -l approach given in the original question) and incorrect: you can see this by inverting the match (git shortlog | grep -Ev '^[ ]+\w+') and seeing that e.g. commits with no message (i.e., "<none>") are not counted. Using git rev-list HEAD --count is both more succinct and more accurate.Kettledrum
@Kettledrum if you have a better solution please post your own answer. Don't post pedantic comments to everyone else's. Based on what your saying, mine will fail less than one percent of the time. It's very rare not to have a message in a commit.Saberio
@Kettledrum The earliest reference to your solution seems to be from last year. I think it must be a newer feature. My answer was posted in 2010. I don't see any good reason for you to post a snarky comment to my answer.Saberio
@BenAtkin: My apologies; it was not my intent to be offensive, merely factual. Point taken about the date of the response. At the time, your solution may very well have been the best available one. But I stand by my statement that git rev-list HEAD --count is a better solution now.Kettledrum
@Kettledrum OK, I see how at this point a new answer is unlikely to get voted to the top, and why you used this strategy instead of adding a new answer. I still think you ought to try both, though. I added your answer to the top of mine.Saberio
How to get the number of commits on the remote repository/branch?Locular
In addition, git rev-list --all --count works for all branches.Levesque
Added an answer as well and works also with old versions: git log --oneline | wc -lPediatrics
rev-list will return count of only those commits which are reachable from HEAD, not all commits.Marya
@Marya from the question: "The goal is to use it as a unique, incrementing build number." It also mentions SVN. I think using the count for the branch the build is being done on is appropriate. If it isn't HEAD, just change HEAD to the name of the branch.Saberio
@Marya thanks! updated the answer. also de-emphasized the body of the question post. I think most are looking for answers to the title of the question post.Saberio
git config --global alias.count "rev-list --count" for short git countErepsin
If you use the count for all branches, activity on other branches could change the number - How and when is it possible?Congeneric
It's worth noting that if anyone ever force pushes a different commit to the branch then it'll have the same identifier but will be different code. E.g. you roll back code by force pushing backwards, then re-add a fixed version of a previous commit. To use this system for build identifiers you always need to roll forwards.Afrikander
Also, git rev-list --count $(git branch --show-current) displays the count for the current branch.Whitesmith
P
209

git shortlog is one way.

Providenciaprovident answered 24/3, 2009 at 13:39 Comment(6)
Ty. This worked for me when counting commits in a range; git shortlog sha1..sha2Varlet
Yep, the first line of git shortlog has the number of commits in it. Problem solved.Kym
The number of commits is grouped by committer, not so good. Can count lines in git shortlog, but this doesn't work over ssh without a terminal for some reason (pager?). The asker's original solution is the best! git log --pretty=format:'' | wc -lAldora
I agree; git shortlog by itself does not address the original question of total number of commits (not grouped by author).Kettledrum
However, I would suggest git rev-list HEAD --count rather than the original approach given in the OP. In my tests, git log --pretty=format:'' | wc -l is off by one.Kettledrum
@Kettledrum git log --oneline | wc -l isn't off by one (OS X 10.8.5).Quote
G
170

This command returns count of commits grouped by committers:

git shortlog -s

Output:

14 John lennon
9  Janis Joplin

You may want to know that the -s argument is the contraction form of --summary.

Genteel answered 14/11, 2011 at 7:52 Comment(3)
git shortlog by itself does not address the original question of total number of commits (not grouped by author). Use git rev-list HEAD --count instead.Kettledrum
Awesome! You can sort it by | sort -n tooJuarez
@Juarez or simply git shortlog -snChari
E
138

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.

Entire answered 9/3, 2013 at 19:43 Comment(0)
H
58

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

Harday answered 24/3, 2009 at 14:8 Comment(6)
That could work and would be more easy to use than a custom-made algo. +1Sothena
I didn't know git describe. This little number between the tag name and the sha1 is just what I was looking for. Thank you.Sterilize
Take a look at GIT-VERSION-GEN script and how it is used in git repository, and similar script in Linux kernel sources (and how they are used in Makefile).Mattson
This gives unique, but not INCREMENTAL id. Doesn't work for me. However Ben Atkin's answer offers commit count, which in practice should be incremental. Aaron Digulla's answer is more sure, but requires also more work.Golda
Yes, that’s because the concept of an incremental ID does not make any sense with distributed version control systems.Harday
If you get fatal: No names found, cannot describe anything, you need to create at least one tag (the count will start from that tag). If the tag is not annotated you should do git describe --tagsElissa
W
47

U can just use :

git shortlog -s -n

Result :

 827  user one
    15  user two
     2  Gest 
Wares answered 26/12, 2017 at 9:9 Comment(1)
I like this, it's concise and sorts by number of commitsBlinking
S
34

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.

Sothena answered 24/3, 2009 at 13:54 Comment(3)
I've updated my script to work with Xcode 3. You can pick up an up to date version from gist.github.com/208825.Noodlehead
any way to have the count include the squashed commits as well? i.e. if you squashed 10 commits into 1, is there a way to have it counted as 10, not 1?Longlimbed
@Honey I don't think it is possible with git describe alone.Sothena
P
26

A simple way is:

 git log --oneline | wc -l

oneline ensures that.

Pediatrics answered 6/2, 2014 at 15:38 Comment(3)
'wc' is not recognized as an internal or external command, operable program or batch file.Oud
Well what system are you using? Is it a UNIX one ?/Pediatrics
This seems faster too if you have thousands of commits. All other commands take too much time.Whaler
P
23

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

export GIT_REV_COUNT=`git rev-list --all --count`
Postbellum answered 15/7, 2011 at 21:52 Comment(5)
Indeed, git rev-list is the correct tool to use, not git log like the other say.Gass
To count the number of commits in the lineage to reach HEAD: git rev-list --first-parent | wc -lProtuberancy
You don't need wc -l just use the --count switch: git rev-list --all --count.Adjudicate
Thanks @slm, I've updated the answer. Although, I suspect the original answer is older than the --count switch itself.Postbellum
@JohnGietzen - oh yeah I figured that 8-), was just adding this detail to help.Adjudicate
S
19

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.

Steenbok answered 25/11, 2012 at 7:59 Comment(1)
git shortlog by itself does not address the original question of total number of commits (not grouped by author). Use git rev-list HEAD --count instead.Kettledrum
H
13

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.

EDIT: Get count between two commits:


I was looking for an answer that would show how to get the number of commits between two arbitrary revisions and didn't see any.

git rev-list --count [older-commit]..[newer-commit]
Handout answered 26/10, 2012 at 14:50 Comment(2)
Thanks for the alias suggestion. For whoever thinks it is usefull: I like to easily add aliases via a command: git config --global alias.revno '!git rev-list --full-history --all | wc -l'Fy
Altho you coult also make the command a bit shorter by using --count like so: git config --global alias.revno '!git rev-list --full-history --all --count'Fy
B
13

there are couple of cool methods to do so -

  • First method

git shortlog -s

This command prints a list of commits count by all users who contributed to the repo.

956 Pankaj Tanwar
235 The Ninja
540 The Hardcore Geek
664 The Ever Shining Star
984 The Experienced Man

Simply, to get the count of total commits -

git shortlog -s | grep "Pankaj Tanwar"

it prints -

956 Pankaj Tanwar
  • Another clean and cool method is -
git rev-list HEAD --author="Pankaj Tanwar" --count 

To calculate total lines of code contributed & total pull requests raised, check this blog

Biography answered 18/1, 2022 at 7:26 Comment(1)
This does not give a count of total commits because it filters by author.Foust
R
7

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

Radman answered 24/3, 2009 at 15:22 Comment(0)
T
7

The following command prints the total number of commits on the current branch.

git shortlog -s -n  | awk '{ sum += $1; } END { print sum; }' "$@"

It is made up of two parts:

  1. Print the total logs number grouped by author (git shortlog -s -n)

    Example output

      1445  John C
      1398  Tom D
      1376  Chrsitopher P
       166  Justin T
       166  You
    
  2. Sum up the total commit number of each author, i.e. the first argument of each line, and print the result out (awk '{ sum += $1; } END { print sum; }' "$@")

    Using the same example as above it will sum up 1445 + 1398 + 1376 + 166 + 166. Therefore the output will be:

      4,551
    
Transatlantic answered 16/10, 2020 at 10:12 Comment(1)
Good solution! What would an alias look like for this command? And what are the benefits for this usage in comperison to git rev-list HEAD | wc -l?Fy
O
6

git rev-parse --short HEAD

Onagraceous answered 24/3, 2009 at 13:39 Comment(1)
Produces a hash, whereas a number was requested.Nominate
N
4

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.

Nominate answered 17/9, 2014 at 20:51 Comment(0)
T
3

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.

Tardif answered 24/3, 2009 at 13:59 Comment(3)
If two distributed developers did this wouldn't their build numbers collide/intersect periodically? What if they both did a build between the same revs of a shared repo, or perhaps collision would only occur if either had changes not committed to the shared repo. Not sure.Tutti
Sure but the conflict tells you what to do: Just talk to the other guy or always use higher number. Remember: A number can't magically cure a broken build process. It's just a reminder or hint that you need to check something.Tardif
Ahh, yes, the magic buildno.txt file is committed along with the rest. Good approach for a small team, or a large team that avoids parallel builds. Only place I can think of that it might not work as well is for a large team using a scripted language (python) that doesn't need a build process (to assign a single person to do building).Tutti
S
3

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)
Shier answered 11/6, 2014 at 11:5 Comment(0)
S
3

git shortlog by itself does not address the original question of total number of commits (not grouped by author)

That is true, and git rev-list HEAD --count remains the simplest answer.

However, with Git 2.29 (Q4 2020), "git shortlog"(man) has become more precise.
It has been taught to group commits by the contents of the trailer lines, like "Reviewed-by:", "Coauthored-by:", etc.

See commit 63d24fa, commit 56d5dde, commit 87abb96, commit f17b0b9, commit 47beb37, commit f0939a0, commit 92338c4 (27 Sep 2020), and commit 45d93eb (25 Sep 2020) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 2fa8aac, 04 Oct 2020)

shortlog: allow multiple groups to be specified

Signed-off-by: Jeff King

Now that shortlog supports reading from trailers, it can be useful to combine counts from multiple trailers, or between trailers and authors.
This can be done manually by post-processing the output from multiple runs, but it's non-trivial to make sure that each name/commit pair is counted only once.

This patch teaches shortlog to accept multiple --group options on the command line, and pull data from all of them.

That makes it possible to run:

git shortlog -ns --group=author --group=trailer:co-authored-by  

to get a shortlog that counts authors and co-authors equally.

The implementation is mostly straightforward. The "group" enum becomes a bitfield, and the trailer key becomes a list.
I didn't bother implementing the multi-group semantics for reading from stdin. It would be possible to do, but the existing matching code makes it awkward, and I doubt anybody cares.

The duplicate suppression we used for trailers now covers authors and committers as well (though in non-trailer single-group mode we can skip the hash insertion and lookup, since we only see one value per commit).

There is one subtlety: we now care about the case when no group bit is set (in which case we default to showing the author).
The caller in builtin/log.c needs to be adapted to ask explicitly for authors, rather than relying on shortlog_init(). It would be possible with some gymnastics to make this keep working as-is, but it's not worth it for a single caller.

git shortlog now includes in its man page:

--group=<type>

Group commits based on <type>. If no --group option is specified, the default is author. <type> is one of:

  • author, commits are grouped by author
  • committer, commits are grouped by committer (the same as -c)

This is an alias for --group=committer.

git shortlog now also includes in its man page:

If --group is specified multiple times, commits are counted under each value (but again, only once per unique value in that commit). For example, git shortlog --group=author --group=trailer:co-authored-by counts both authors and co-authors.

Sothena answered 9/10, 2020 at 20:12 Comment(0)
K
2

The one I used to use was:

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

Simple but it worked.

Kym answered 31/7, 2011 at 14:24 Comment(2)
it takes one commit message line begining with "commit" to break count. For example: "corrected mistakes and broken tests that I accidentally pushed in last\ncommit"Anaphase
this is not 100% bullet-proof if you have a a commit message like @PawełPolewicz, but it works the best in my opinion.Transatlantic
E
2

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
Edwardoedwards answered 15/11, 2016 at 10:24 Comment(0)
E
1

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

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

git count

Ehlers answered 11/9, 2017 at 9:46 Comment(0)
K
1

How about making an alias ?

alias gc="git rev-list --all --count"      #Or whatever name you wish
Karoline answered 23/1, 2020 at 15:14 Comment(0)
J
1

To get the number of commits that differ between two branches e.g. a feature branch and a target use:

git rev-list --count feature_branch..target_branch

Jittery answered 11/1, 2023 at 14:57 Comment(0)
L
1

You can try these in git bash

  1. git log --author="Your-Email-or-Name" --pretty=oneline

this gives you a list

  1. git log --author="Your-Email-or-Name" --pretty=oneline | wc -l

this gives you a count

Lumbering answered 1/9, 2023 at 10:18 Comment(0)
S
0

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"

Schoolbook answered 5/11, 2015 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.