Skip to main content
Chapter 48 of 61 · 8 sections · Page 162

Git: Git Statistics

Analyze commit counts, lines of code per author, commit dates, and find local repositories.

Original chapter reference: Chapter 48: Git statistics. The command options and documentation recipes below are fully transcribed from Git Notes for Professionals.

Parameter Details Sort output according to the number of commits per author instead of

Source example · PDF page 162
-n,--numbered

alphabetic order -s, --summary Only provide a commit count summary -e, --email Show the email address of each author Instead of the commit subject, use some other information to describe each --format[=<format>] commit. <format> can be any string accepted by the --format option of git log. Linewrap the output by wrapping each line at width. The first line of each -w[<width>[,<indent1>[,<indent2>]]] entry is indented by indent1 number of spaces, and subsequent lines are indented by indent2 spaces. Show only commits in the specified revision range. Default to the whole <revision range> history until the current commit. Show only commits that explain how the files matching path came to be. [--] <path> Paths may need to be prefixed with "-- " to separate them from options or the revision range.

Section 48.1: Lines of code per developer

Source example · PDF page 162
git ls-tree -r HEAD | sed -Ee 's/^.{53}//' | \
while read filename; do file "$filename"; done | \
grep -E ': .*text' | sed -E -e 's/: .*//' | \
while read filename; do git blame --line-porcelain "$filename"; done | \
sed -n 's/^author //p' | \
sort | uniq -c | sort -rn

Section 48.2: Listing each branch and its last revision's date

Source example · PDF page 162
for k in `git branch -a | sed s/^..//`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci
%Cblue%cr%Creset" $k --`\\t"$k";done | sort

Section 48.3: Commits per developer

Git shortlog is used to summarize the git log outputs and group the commits by author.

By default, all commit messages are shown but argument --summary or -s skips the messages and gives a list of authors with their total number of commits.

--numbered or -n changes the ordering from alphabetical (by author ascending) to number of commits descending.

Source example · PDF page 162
git shortlog -sn        #Names and Number of commits

git shortlog -sne       #Names along with their email ids and the Number of commits

or

Source example · PDF page 162
git log --pretty=format:%ae \
| gawk -- '{ ++c[$0]; } END { for(cc in c) printf "%5d %s\n",c[cc],cc; }'

Note: Commits by the same person may not be grouped together where their name and/or email address has been spelled differently. For example John Doe and Johnny Doe will appear separately in the list. To resolve this, refer to the .mailmap feature.

Section 48.4: Commits per date

Source example · PDF page 163
git log --pretty=format:"%ai" | awk '{print " : "$1}' | sort -r | uniq -c

Section 48.5: Total number of commits in a branch

Source example · PDF page 163
git log  --pretty=oneline |wc -l

Section 48.6: List all commits in pretty format

Source example · PDF page 163
git log --pretty=format:"%Cgreen%ci %Cblue%cn  %Cgreen%cr%Creset %s"

This will give a nice overview of all commits (1 per line) with date, user and commit message.

The --pretty option has many placeholders, each starting with %. All options can be found here

Section 48.7: Find All Local Git Repositories on Computer

To list all the git repository locations on your you can run the following

Source example · PDF page 163
find $HOME -type d -name ".git"

Assuming you have locate, this should be much faster:

Source example · PDF page 163
locate .git |grep git$

If you have gnu locate or mlocate, this will select only the git dirs:

Source example · PDF page 163
locate -ber \\.git$

Section 48.8: Show the total number of commits per author

In order to get the total number of commits that each developer or contributor has made on a repository, you can simply use the git shortlog:

Source example · PDF page 163
git shortlog -s

which provides the author names and number of commits by each one.

Additionally, if you want to have the results calculated on all branches, add --all flag to the command:

Source example · PDF page 163
git shortlog -s --all
Buy me a coffee
Daily Practice
0/0