You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何按指定作者统计Git仓库年度代码变更总行数?附2019年需求

Hey there! Let's tackle this 2019 code stats task—you don't need those random unfiltered scripts; Git has all the tools you need built right in. Here's a step-by-step breakdown to get exactly what you're after:

1. Get Total Code Lines for 2019

This command will calculate the total lines added, removed, and net change across all 2019 commits:

git log --all --since="2019-01-01" --until="2019-12-31" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "Total lines added: %s\nTotal lines removed: %s\nTotal net lines: %s\n", add, subs, loc }'

Let me break this down:

  • --all: Checks all branches (remove this if you only want a specific branch like main)
  • --since/--until: Locks the date range to exactly 2019
  • --numstat: Outputs a line with added/removed lines for each file in every commit
  • The awk script sums up all the numbers to give you total stats
2. Group Stats by User for 2019

To get a breakdown per team member, use this command—it will show net lines, added lines, and removed lines for each contributor:

git log --all --since="2019-01-01" --until="2019-12-31" --pretty=tformat: --numstat | awk '{ users[$3] += $1 - $2; add[$3] += $1; sub[$3] += $2 } END { for (user in users) { printf "%s:\n  Net lines: %s\n  Lines added: %s\n  Lines removed: %s\n", user, users[user], add[user], sub[user] } }'

Key notes here:

  • $3 grabs the committer's name from the numstat output
  • We use associative arrays in awk to accumulate stats per user
  • The final loop prints a clean breakdown for each person
3. Pro Tips for Accurate Results
  • Fix username inconsistencies: If team members used different emails/usernames (e.g., "John Doe" vs "jdoe"), create a .mailmap file in your repo root to unify them. Example:
    John Doe <john.doe@company.com> jdoe <jdoe@old-email.com>
    
    Git will use the first name/email for stats after this.
  • Exclude auto-generated files: Skip folders like node_modules, dist, or log files by adding -- . ':!node_modules' ':!dist' to the end of your git log command. Example:
    git log --all --since="2019-01-01" --until="2019-12-31" --pretty=tformat: --numstat -- . ':!node_modules' ':!dist' | awk ...
    
  • Filter by specific branches: Replace --all with branch names (e.g., --main --develop) if you don't want to include feature branches that never merged.

内容的提问来源于stack exchange,提问作者Matt

火山引擎 最新活动