29

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

5 Answers 5

69

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}'
Sign up to request clarification or add additional context in comments.

1 Comment

it seems uniq is not recognized in powershell. It works for me on windows 11 only in wsl
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

Comments

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 Comments

Combine with grep "your date". This only works for one specific day. The solution above has ranges.
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.
Also, more simply, what if a commit message contains the word Date...?
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 Comments

What does v2.37.0.. in the first command mean? It does not seem to work...
@JonasKello It is a tag from the git/git repository.
1

As seen in the answer by @underscore_d, with the following command, you can request the number of commits submitted on a daily basis.

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

Utilizing this, I have prepared the commands necessary for monthly and yearly queries.

Months

Using the awk command, keep only the 6th and 7th characters from the date, representing the numbers indicating the month.

git log --date=short --pretty=format:%ad | awk '{print substr($1, 6, 2)}' | sort | uniq -c

Example Result

12 01
60 02
12 03
13 04
10 05
 4 06
24 07
30 08
10 09
 2 10
89 11
46 12

Yearly Months

Using the awk command, keep only the first 7 characters from the date, representing the numbers indicating the year and month.

git log --date=short --pretty=format:%ad | awk '{print substr($1, 1, 7)}' | sort | uniq -c

Example Result

 1 2017-01
 6 2017-02
12 2017-03
13 2017-04
10 2017-05
 7 2018-01
 5 2018-02
13 2019-08
 4 2020-02
14 2020-07
 2 2020-11
 1 2021-01

Years

Using the awk command, keep only the first 4 characters from the date, representing the numbers indicating the year.

git log --date=short --pretty=format:%ad | awk '{print substr($1, 1, 4)}' | sort | uniq -c

Example Result

19 2017
42 2018
89 2019
 3 2020

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.