Git: generate statistics with shortlog

Measurements, measurements.

git shortlog generate statistics about the commits in a repository, intended to help generate project release notes. By default, it groups commits by author, but it can use other fields too. Use it for a more powerful dissection of a repository than tools like GitHub’s Insights tab.

This post covers some likely helpful use cases.

Commits by author

To see all commits in the last month, grouped by author:

$ git shortlog -n --since '1 month ago'
Henley 'One Leg' Morrow (53):
      Added magic to the code (#8391)
      Who needs testing when we have coffee? (#8390)
      ...

Xignus (28):
      Make widget factory work on Tuesdays (#8272)
      Because you can never have too many cats (#8344)
      ...

...

-n (--numbered) sorts by the commit counts rather than the author name.

The --since option limits the log to the commits created in the last month. Drop it to see commits from all time or combine with --until to limit the end point.

To hide commit details and show only the counts per author, add -s (--summary):

$ git shortlog -ns --since '1 month ago'
    53  Henley 'One Leg' Morrow
    28  Xignus
    13  Cedar Flattertwinkle
    10  Zujaszuc
     8  Ojaguu

Limit by commit range

git shortlog accepts the same commit-limiting options that git log does. This includes the .. notation for commit ranges. For example, to check all commits between two release tags:

$ git shortlog -ns v1.77.0..v1.78.0
    22  Henley 'One Leg' Morrow
    10  Xignus
     5  Zujaszuc

Limit by pathspec

Again, like git log, you can restrict git shortlog to specific paths with a pathspec, Git’s syntax for matching file paths. For example, to show who wrote commits that touched HTML files:

$ git shortlog -ns --since '1 month ago' -- '*.html'
     8  Cedar Flattertwinkle
     4  Xignus
     2  Ojaguu

Or to limit to a subdirectory:

$ git shortlog -ns --since '1 month ago' -- solitude/core
    16  Henley 'One Leg' Morrow
     5  Xignus
     3  Cedar Flattertwinkle

Count authors and coauthors

The default grouping is per author, which excludes work done by coauthors, as recorded in co-authored-by “trailers”. To count both, provide --group twice, once for each field:

$ git shortlog -ns --group=author --group=trailer:co-authored-by --since '1 month ago'
    54  Henley 'One Leg' Morrow
    38  Otectron
    29  Xignus
    16  Cedar Flattertwinkle
    10  Zujaszuc
     8  Ojaguu

This is probably the fairest way of counting work done since it surfaces pair programmers and feedback from code review. In the above examples, “Otectron” never appeared as an author but has co-authored 38 commits in the last month. That’s substantial work that was otherwise hidden!

Commits by day

Grouping can be done by any field or set of fields formatted from the commit. For example, to check commits per day, use group='format:%cd' (committer date), with --date to select date formatting:

$ git shortlog -s --date='format:%Y-%m-%d' --group='format:%cd' --since '1 month ago'
     3  2024-08-03
     5  2024-08-05
     8  2024-08-06
     3  2024-08-07
     ...

Some other useful date/time groupings:

Comits by an author

The above date/time stats may be more insightful when restricted to just one author, perhaps yourself. Pass a name, or just the first part, to --author to do this:

$ git shortlog -s --date='format:%H' --group='format:%cd' --since '1 month ago' --author Henley
 3  07
 1  08
 5  09
 3  10
 5  11
 2  13
 ...

Looks like 9 and 11 am are productive times for Henley.

Fin

May your journey be guided by insight and awareness,

—Adam


😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: