KKAIROS
Practical guideIllustrative3 min read

Git Shortlog: commits per author

git shortlog -sn counts commits per author (-s) and sorts by number of commits instead of by name (-n). The docs do not state the direction. In this run the author with more commits came first.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-72-A: shortlog -sn lists authors with commit counts; the run shows the larger count first

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'b\n' >> f.txt; git commit -qam two
printf 'c\n' >> f.txt; GIT_AUTHOR_NAME=other git commit -qam three

Command:

git shortlog -sn HEAD | sed 's/^ *//'

Recorded output:

2	tester
1	other

Exit status: 0. The output matches the expected text written before the run. This is the status of sed, the last command in the pipe.

TE-72-B: shortlog on an older revision counts only the commits reachable from it

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'b\n' >> f.txt; git commit -qam two
printf 'c\n' >> f.txt; GIT_AUTHOR_NAME=other git commit -qam three

Command:

git shortlog -sn HEAD~1 | sed 's/^ *//'

Recorded output:

2	tester

Exit status: 0. This status and the output match what was written before the run. The status is that of sed, the last command in the pipe.

TE-72-C: expected non-zero exit: shortlog with a revision name that does not exist stops with a fatal error

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one

Command:

git shortlog -sn nosuchrev

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: ambiguous argument 'nosuchrev': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Exit status: 128. This status and the output match what was written before the run.

TE-72-B: by the setup, the history has two commits by tester and a newest commit by other. HEAD~1 leaves out the newest commit, so the only line is 2 tester. TE-72-C: git shortlog -sn nosuchrev exited with status 128. The first line of standard error is fatal: ambiguous argument 'nosuchrev': unknown revision or path not in the working tree.

Limits

Authors are grouped by name. The same person with two different names is counted twice (not tested). The run sets author names through environment variables.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-72-A | shortlog -sn lists authors with commit counts; the run shows the larger count first | 0 | PASS | 6a19e498c0f7 | | TE-72-B | shortlog on an older revision counts only the commits reachable from it | 0 | PASS | 7fefad0cb1d1 | | TE-72-C | expected non-zero exit: shortlog with a revision name that does not exist stops with a fatal error | 128 | PASS | 9d8f5a780033 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides