Git commit count a day

10,021

Solution 1

This answers the "per day" side of the question asked, which can't seem to decide whether it wants "per/each day" implying multiple or just "a day" implying single. ;-) Obviously, "per day" is a superset of "a day", so that's the one that's useful to show; grep and such can do the rest!

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.

comedy bonus: 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

sed 's:^  *\([1-9][0-9]*\) \([1-9][0-9-]*\)$:\2\t\1:g'

It's that simple...!

Alternatively, avoid going mad by just using awk instead of sed:

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

Solution 2

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
Share:
10,021
Sanjay Dutt
Author by

Sanjay Dutt

Updated on June 04, 2022

Comments

  • Sanjay Dutt
    Sanjay Dutt about 2 years

    I have a branch called development. Now I want to know how many commits are happened per day (i.e) each day.

    I want Toal number of commits (i.e) count of commits in a day.

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

    git shortlog -s -n
    

    My question is count number of commits in a day