Counting accumulated changes in Git

It's almost christmas and our team is tasked with sharing some highlights this year. We thought sharing how much code we've written is a good idea. Sure it's not a good metric for measuring productivity, but good enough for bragging rights. But.. how? git diff --shortstat works but I don't exactly know which commit happened 1 year ago. Hmm..

Not sure if this is the best method. But here's what I did.

git log --since='1 year ago' --stat | awk -F, '/files changed/{print $2; if($3 != "") print $3}' | awk -F' ' '{if($2 == "insertions(+)") added += $1; else deleted += $1} END {print "Added: " added ", deleted " deleted}'

How it works

First, git log --since='1 year ago' --stat gives you the added and deleted lines for each commit. Then an awk script is used to extract the added and deleted chunk into separate lines. Take the output I got from my GNUnet++ project as an example:

❯ git log --since='1 year ago' --stat
commit d0553a29fcc1b981ae4889d914f98b78147ab20a (HEAD -> master)
Author: marty1885 <marty188586@gmail.com>
Date:   Sun Dec 18 12:53:54 2022 +0800

    use smart pointer to handle connection lifetime

 examples/cadet/main.cpp     |  6 +++---
 gnunetpp/gnunetpp-cadet.cpp | 29 ++++++++++++++++++-----------
 gnunetpp/gnunetpp-cadet.hpp | 17 +++++++++--------
 3 files changed, 30 insertions(+), 22 deletions(-)

commit e6c029f58b5f7910235e4daf4fda8aded2992dc1
Author: marty1885 <marty188586@gmail.com>
Date:   Sun Dec 18 12:16:41 2022 +0800

    make all services non-copyable

 gnunetpp/gnunetpp-cadet.hpp | 4 +---
 gnunetpp/inner/Infra.hpp    | 3 ++-
 2 files changed, 3 insertions(+), 4 deletions(-)

commit 4275294bb3e1cbb1cbe3088a9b94f5908dc0092c
Author: marty1885 <marty188586@gmail.com>
Date:   Sun Dec 18 11:46:27 2022 +0800
...

❯ git log --since='1 year ago' --stat | awk -F, '/files changed/{print $2; if($3 != "") print $3}'
 30 insertions(+)
 22 deletions(-)
 3 insertions(+)
 4 deletions(-)
 23 insertions(+)
 7 deletions(-)
 ...

The final awk script is used to sum up the added and deleted lines.

❯ git log --since='1 year ago' --stat | awk -F, '/files changed/{print $2; if($3 != "") print $3}' | awk -F' ' '{if($2 == "insertions(+)") added += $1; else deleted += $1} END {print "Added: " added ", deleted: " deleted}'
Added: 5754, deleted: 1306

I probably should have used git log --since='1 year ago' | tail and git diff on the last commit. But somehow I never go down that route. This works good enough for me.

Author's profile. Photo taken in VRChat by my friend Tast+
Martin Chang
Systems software, HPC, GPGPU and AI. I mostly write stupid C++ code. Sometimes does AI research. Chronic VRChat addict

I run TLGS, a major search engine on Gemini. Used by Buran by default.


  • marty1885 \at protonmail.com
  • Matrix: @clehaxze:matrix.clehaxze.tw
  • Jami: a72b62ac04a958ca57739247aa1ed4fe0d11d2df