Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some commands to get git commit log statistics for a repository on the command line.

git commit stats

Commands to get commit statistics for a Git repository from the command line -
using git log, git shortlog and friends.




List repository contributors by author name (sorted by name):

$ git log --format='%aN' | sort -u 

Example output:

Jane Bar
John Foo
Steve Baz



List total commits by author (sorted by commit count):

$ git shortlog -sn

Example output:

136 Jane Bar
 41 John Foo
 17 Steve Baz

Ignore merge commits:

$ git shortlog -sn --no-merges

Example output:

121 Jane Bar
 36 John Foo
 14 Steve Baz

Even though the --no-merges option is not documented for git shortlog, it works exactly as defined for git log.




List file change stats by author:

$ git log --author="Vorname Nachname" --pretty=tformat: --numstat | awk '{inserted+=$1; deleted+=$2; delta+=$1-$2; ratio=deleted/inserted} END {printf "Commit stats:\n- Lines added (total)....  %s\n- Lines deleted (total)..  %s\n- Total lines (delta)....  %s\n- Add./Del. ratio (1:n)..  1 : %s\n", inserted, deleted, delta, ratio }' -

Example output:

Commit stats:
- Lines added (total)....  4625
- Lines deleted (total)..  836
- Total lines (delta)....  3789
- Add./Del. ratio (1:n)..  1 : 0.180757

Include file count:

$ git log --shortstat --author="Vorname Nachname" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "Commit stats:\n- Files changed (total)..  %s\n- Lines added (total)....  %s\n- Lines deleted (total)..  %s\n- Total lines (delta)....  %s\n- Add./Del. ratio (1:n)..  1 : %s\n", files, inserted, deleted, delta, ratio }' -

Example output:

Commit stats:
- Files changed (total)..  439
- Lines added (total)....  4625
- Lines deleted (total)..  836
- Total lines (delta)....  3789
- Add./Del. ratio (1:n)..  1 : 0.180757

Ignore merge commits:

Note: Both commands above also count merge commits. But to ignore them, one can simply use the --no-merges option again:

$ git log --author="Vorname Nachname" --pretty=tformat: --numstat --since="1 Jan, 2015" | awk ...
# or
$ git log --shortstat --author="Vorname Nachname" --since="1 Jan, 2015" | grep -E ...

Filter stats by date:

You can filter the output of the above commands, for example, by adding --until or --since or --before:

$ git log --author="Vorname Nachname" --pretty=tformat: --numstat --since="1 Jan, 2015" | awk ...
# or
$ git log --shortstat --author="Vorname Nachname" --since="1 Jan, 2015" | grep -E ...






@stevenirby

This comment has been minimized.

Copy link

stevenirby commented May 31, 2017

You can also skip the name bit and pull it from your config.

git log --shortstat --author="$(git config user.name)" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "Commit stats:\n- Files changed (total)..  %s\n- Lines added (total)....  %s\n- Lines deleted (total)..  %s\n- Total lines (delta)....  %s\n- Add./Del. ratio (1:n)..  1 : %s\n", files, inserted, deleted, delta, ratio }' -

$(git config user.name)

@Bird87ZA

This comment has been minimized.

Copy link

Bird87ZA commented Oct 19, 2017

How would I print something like this to a file for a whole git project?

Date; Author; Commit Message; Additions; Deletions; Ratio;

@OtiZ2

This comment has been minimized.

Copy link

OtiZ2 commented Mar 5, 2018

If i want add this command as alias, there is problem, could you help me?

st = log --shortstat --author="$(git config user.name)" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "Commit stats:\n- Files changed (total).. %s\n- Lines added (total).... %s\n- Lines deleted (total).. %s\n- Total lines (delta).... %s\n- Add./Del. ratio (1:n).. 1 : %s\n", files, inserted, deleted, delta, ratio }'
`
juraj@OtiZ-PC MINGW64 /c/wamp64/www
$ git st
fatal: Bad alias.st string: unclosed quote
juraj@OtiZ-PC MINGW64 /c/wamp64/www
$

`

@glebmtb

This comment has been minimized.

Copy link

glebmtb commented Sep 13, 2018

#!/usr/bin/env bash
from="1 Jun, 2018"
to="17 Aug, 2018"
users=$(git shortlog -sn --no-merges --since="$from" --before="$to" | awk '{printf "%s %s\n", $2, $3}')
IFS=$'\n'
echo -e "User name;Files changed;Lines added;Lines deleted;Total lines (delta);Add./Del. ratio (1:n);Commit count"

for userName in $users
do
     result=$(git log --author="$userName" --no-merges --shortstat  --since="$from" --before="$to" | grep -E "fil(e|es) changed" | awk '{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; ratio=deleted/inserted} END {printf "%s;%s;%s;%s;%s", files, inserted, deleted, delta, ratio }' -)
     countCommits=$(git shortlog -sn --no-merges  --since="$from" --before="$to" --author="$userName" | awk '{print $1}')
     if [[ ${result} != ';;;;' ]]
     then
        echo -e "$userName;$result;$countCommits"
     fi
done

sh script for print all users by date

@ifehrim

This comment has been minimized.

Copy link

ifehrim commented Sep 30, 2018

its good

@arzzen

This comment has been minimized.

Copy link

arzzen commented Sep 17, 2019

hi, i am creator of this cli tool https://github.com/arzzen/git-quick-stats
"Git quick statistics" is a simple and efficient way to access various statistics in git repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.