Skip to content

Instantly share code, notes, and snippets.

@pstadler
Last active February 9, 2023 13:45
Show Gist options
  • Save pstadler/4722416 to your computer and use it in GitHub Desktop.
Save pstadler/4722416 to your computer and use it in GitHub Desktop.
Print Git commit statistics for a specific author.
# Print Git commit statistics for a specific author
# Usage: git-stats "Linus Torvalds"
git-stats() {
author=${1-`git config --get user.name`}
echo "Commit stats for \033[1;37m$author\033[0m:"
git log --shortstat --author $author -i 2> /dev/null \
| grep -E 'files? changed' \
| awk 'BEGIN{commits=0;inserted=0;deleted=0} \
{commits+=1; if($5!~"^insertion") { deleted+=$4 } \
else { inserted+=$4; deleted+=$6 } } END \
{print "\033[1;34m↑↑\033[1;37m", commits \
"\n\033[1;32m++\033[1;37m", inserted, \
"\n\033[1;31m--\033[1;37m", deleted, "\033[0m"}'
}
@dreamyguy
Copy link

This works great as long as there are insertions. In the case where there would be only deletions, they would count as insertions, because the 4th word on 8 files changed, 81 deletions is deletion, not insertion. That would explain the difference between the stats one gets on Github vs this solution. Your script's approach is popular and there are a few variations sprinkled around stackoverflow, but on a repo with a long history the differences can be quite noticeable, specially if a lot of files are deleted. I have yet to stumble on an improved solution, though... Cheers!

@pstadler
Copy link
Author

@dreamyguy Thanks for your comment, this is now fixed.

@sinhanurag
Copy link

Can this script also handle merge use cases? For instance I merge the remote repo into my branch and then commit my changes. In such a case would the lines inserted deleted only by me be counted pr the merge ones will also be included in the stats?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment