How do you display the number of lines added or deleted in each commit, aggregated by author?
Posted by GraceDv
Last Updated: July 12, 2024
To display the number of lines added or deleted in each commit, aggregated by author, you can use git log along with some command-line tools. Here’s a detailed approach using various command-line utilities.
Using git log with --stat
You can run the following command in your Git repository:
git log --author=<author_name> --stat --pretty=format:"%h %an %ad" --date=short | grep -E "insertions|deletions"
Using git log with --shortstat
To get a clearer output that specifically shows the changes, you can use:
git log --shortstat --pretty=format:"%h %an %ad" --date=short
Using awk to Aggregate by Author
To aggregate the results by author and get the total numbers of lines added and deleted, you might want to use a command like this:
git log --shortstat --pretty=format:"%an" | awk '
    /^Author:/ { author = $2; }
    /insertions/ { added += $4; }
    /deletions/ { deleted += $6; }
    END { printf "%s: +%d -%d\n", author, added, deleted; }'
More Detailed Aggregation
If you want a more structured summarization (for multiple authors), you can use git log with a specific format, combined with awk:
git log --numstat --pretty="%an" | \
awk 'NF==3 { added[$3] += $1; deleted[$3] += $2; authors[$3] = $1 }
     END {
         for (file in added) {
             print authors[file] ": +" added[file] " -" deleted[file];
         }
     }'
Full Aggregation Example
Here is a complete command that provides a more comprehensive aggregation by author: ```bash git log --pretty=format:'%ae %an' --numstat | \ awk '{ if (NF==3) { added[$3] += $1; deleted[$3] += $2; authors[$3] = $1; } } END { print "Author Insertions Deletions"; for (a in authors) { printf "%-20s %15