Pankaj Tanwar
Published on

Counting my contributions to a git repo 💻

––– views

Recently, I resigned from my position as a software engineer to try out a different path.

My crooked mind didn't let me enjoy the notice period and started wondering, exactly how many commits or lines of code I contributed while working here for the last 1 year.

My lazy & sluggish friend Bitbucket, was not happy, me using it to figure out count of total commits, lines of code & merged pull requests.

So, I somehow wanted to do it with some sweet git one liners via command line. I know there are ready-made tiny tools for it but I wanted to do it on my own.

Total commits

I found a super easy way to get the commits count.

git shortlog -s

This command prints a list of commits count by all users who contributed to the repo.

956 Pankaj Tanwar
235 The Ninja
540 The Hardcore Geek
664 The Ever Shining Star
984 The Experienced Man

Simply, to get the count of total commits -

git shortlog -s | grep "Pankaj Tanwar"

it prints -

956 Pankaj Tanwar

Shing Lyu has another cool method to get total commits count -

git rev-list HEAD --author="Pankaj Tanwar" --count

git rev-list HEAD gives all commit objects in HEAD and --author="Pankaj Tanwar" --count will get the total commits count by me.

Well to calm down my old colleagues, please note I am not exposing the confidential data. These counts are just dummy.

Total lines of code

Well, after getting total commits, next I wanted to know the exact lines of code contributed. And I felt its quite knotty and intresting.

After trying a couple of things, I came up with this -

git log --author="Pankaj Tanwar" --oneline --shortstat | grep 'insertions(+)' | awk -F ' ' '{ addition+=$4; remove+=$6 } END { print addition, remove; }'

Umm, I know, its seems fearsome. Let me explain it to you.

  • git log gives all commits on the current HEAD and --author="Pankaj Tanwar" filters out commits by me.
commit 1111186d35a54aa3088c3634e6f6b5fe81529f88
Author: Pankaj Tanwar <pankajtanwar@paisabazaar.com>
Date: Tue Dec 5 13:44:24 2022 +0530
fix: oh man, fixing my sins from the last commit
commit 1111175981b23eb2a5469b92d6a92a35203daf39
Author: Pankaj Tanwar <pankajtanwar@paisabazaar.com>
Date: Tue Dec 4 13:33:06 2022 +0530
feat: pushing a fresh new feature! party time.
  • --oneline gets rid of unwanted data such as author, date etc and converts it to one line format & --shortstat beautifies it a bit with insertions-deletions in each commit and gives the output in following format.
1111186d3 fix: oh man, fixing my sins from the last commit
3 files changed, 143 insertions(+), 44 deletions(-)
111117598 feat: pushing a fresh new feature! party time.
16 file changed, 1409 insertion(+), 1 deletion(-)
  • So, now I have total insertions and deletions for each commit, I will use the super power of grep and awk to get the total lines of code contributed.
  • grep 'insertions(+)' filers the lines which have this string and awk -F ' ' '{ addition+=$4; remove+=$6 } END { print addition, remove; }' splits the line with space, picks the 4th column (which is the count of insertions for each commit) and 6th column (deletions) and pipes it to calculate the total sum.
12345 345

A more beautiful method suggested by Shing Lyu is -

git log --author="Pankaj Tanwar" --pretty=tformat: --numstat | grep -v '^-' | awk '{ add+=$1; remove+=$2 } END { print add, remove }'

Some of the things are self explanatory. --pretty=tformat: removes commit hash, author, date and other unwanted fields. --numstat gives us line added and deleted per file, per commit. Something like this...

72 0 README.md
80 9 main.controller.js
6 1 background.js
3 1 package.json

Don't forget, addition/deletion count of images and such files are represented as - - pankaj.jpg. So, to filter this out, we use grep super power of reverse matching. grep -v '^-' means, find lines that don't match this pattern, starting with -.

At the end, same thing, we pipe the values to awk for calculating the total and final output is like -

12345 345

Total pull requests merged.

I tried using Bitbucket API for this but as expected no luck. So, I started looking for some hack to get total count.

I started exploring git log and found that for merged PR, the commit message, looks like this -

99041a85a Merged in pankaj_dev (pull request #3298)

Correct me If you find any technical inaccuracies in this. But i used this logic to get count of my merged PRs.

git log --author="Pankaj Tanwar" --oneline --shortstat | grep "pull request" | awk '{ total+=1;} END { print total; }'

It prints the total count of PRs.


It was a cute fun exercise and a small gift to myself. I enjoyed learning some hidden truths of git. If you have read till here, I guess you will like my other write ups too.