To rank contributors by the number of lines changed (insertions + deletions) in a Git repository, you can use the following command:
git log --pretty="%aN" --numstat | \
awk 'NF==3 {plus[$4]+=$1; minus[$4]+=$2; name=$4} NF!=3 {name=$1} END {for (n in plus) print plus[n]+minus[n], plus[n], minus[n], n}' | \
sort -rn
However, the above command assumes a certain log format and may need adjustment depending on your shell and Git version. Here’s a more robust and commonly used approach:
One-liner to rank contributors by lines changed
git log --pretty="%aN" --numstat | \
awk 'NF==3 {plus[$0]+=$1; minus[$0]+=$2; author=last} NF!=3 {last=$0} END {for (a in plus) print plus[a]+minus[a], plus[a], minus[a], a}' | \
sort -rn
Or, a more readable version:
git log --pretty="%aN" --numstat | \
awk '
NF==3 {plus[author]+=$1; minus[author]+=$2}
NF!=3 {author=$1}
END {
for (a in plus)
printf "%10d lines changed (%6d insertions, %6d deletions): %s\n", plus[a]+minus[a], plus[a], minus[a], a
}
' | sort -rn
Explanation:
git log --pretty="%aN" --numstat: Outputs author names and per-file insertions/deletions.
awk: Sums insertions and deletions per author.
sort -rn: Sorts the results in descending order.
Result:
You’ll get a list of contributors, ranked by total lines changed (insertions + deletions), with a breakdown of insertions and deletions.
Note:
- This counts all history. To limit to a branch or time period, add options to
git log (e.g., git log main --since="2024-01-01").
- For email instead of name, use
--pretty="%aN <%aE>".
Let me know if you want a version for a specific branch or time range!