26

I have a branch called development. Now I want to know how many commits happened per day.

I tried this command, but it is counting all commits from the branch

git shortlog -s -n

4 Answers 4

64

Short and sweet:

git log --date=short --pretty=format:%ad | sort | uniq -c

Example output:

      1 2017-12-08
      6 2017-12-26
     12 2018-01-01
     13 2018-01-02
     10 2018-01-14
      7 2018-01-17
      5 2018-01-18

Explanation:

  • git log is a prerequisite, obviously.
  • --date=short sets our date-format to YYYY-MM-DD, which (A) is all we need and (B) will subsequently alphabetically sort into chronological order.
  • --pretty=format:%ad tells git that we only want to get each commit's author date in our preferred date-format. If you wanted, you could instead use cd for commit date, but that tends to get a lot less useful as soon as you cherry-pick, rebase, etc.
  • | sort is needed for uniq, as it only checks for adjacent duplicates. And of course, we almost certainly want the dates to be ordered at the end anyway.
  • | uniq -c counts the number of adjacent duplicates for each YYYY-MM-DD and prepends that count to the date.

If you want that as a tab-separated date then count, for input into a graphing engine or suchlike, then just pipe the above result into

awk 'BEGIN{OFS = "\t"} {print $2, $1}'
0
10

Try this:

$ git rev-list --count --since=<start-date> --before=<end-date> <ref>

For example, to get the number of commits done yesterday in the current branch:

$ git rev-list --count --since=yesterday --before=today HEAD

Absolute dates are also accepted:

$ git rev-list --count --since=2016-03-02 --before=2016-03-03 HEAD
4

I've tried with:

git log | grep Date | awk '{print " : "$4" "$3" "$6}' | uniq -c

And it works. You'll get something like:

   5  : 3 Mar 2016
   4  : 2 Mar 2016
   8  : 1 Mar 2016
   [...]

I found the command here.

3
  • Combine with grep "your date". This only works for one specific day. The solution above has ranges.
    – user5201742
    Mar 3, 2016 at 13:00
  • This seems unnecessarily wasteful since it pipes a normal, full git log instead of only the minimum it needs per commit. Also, you don't sort, which could lead to uniq returning bad results and/or screaming at you, because neither author (what you show here) nor commit dates need be in chronological order. Jan 18, 2018 at 19:35
  • Also, more simply, what if a commit message contains the word Date...? Nov 23, 2022 at 8:36
2

With Git 2.39 (Q4 2022), you have another option, since "git shortlog"(man) learned to group by the format string.

See commit 7b11234, commit 9c10d4f, commit 10538e2, commit 3dc95e0, commit b017d3d, commit 0b293df (24 Oct 2022) by Taylor Blau (ttaylorr).
See commit 251554c (24 Oct 2022) by Jeff King (peff).
(Merged by Taylor Blau -- ttaylorr -- in commit c112d8d, 30 Oct 2022)

shortlog: support arbitrary commit format --groups

Signed-off-by: Taylor Blau

In addition to generating a shortlog based on committer, author, or the identity in one or more specified trailers, it can be useful to generate a shortlog based on an arbitrary commit format.

This can be used, for example, to generate a distribution of commit activity over time, like so:

$ git shortlog --group='%cd' --date='format:%Y-%m' -s v2.37.0..
   117  2022-06
   274  2022-07
   324  2022-08
   263  2022-09
     7  2022-10

Arbitrary commit formats can be used.
In fact, git shortlog(man)'s default behavior (to count by commit authors) can be emulated as follows:

$ git shortlog --group='%aN <%aE>' ...

and future patches will make the default behavior (as well as --committer, and --group=trailer:<trailer>) special cases of the more flexible --group option.

Note also that the SHORTLOG_GROUP_FORMAT enum value is used only to designate that --group:<format> is in use when in stdin mode to declare that the combination is invalid.

git shortlog now includes in its man page:

git log). Useful with --group=format:<format>.

git shortlog now includes in its man page:

  • format:<format>, any string accepted by the --format option of 'git log'. (See the "PRETTY FORMATS" section of git log.)
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.