[Git] Git统计代码行数

1 前言

今天,有这么一个需求:小组老大要求咱们【每个人】把【上个月】的【代码行数】统计一下并上报。

成,统计就统计,但那么多项目,总不能让我用手去数吧?何况,时间久了,自己也不清楚自己改了哪些地方了。
So?当然是看看有木有直接用Git统计的方法了。(根据作者+时间区间)

百度大法一下,嘿,还真有!用完之后还真爽!避免未来还需要干这事儿,我觉得有必要小结一下,也方便以后复用。

2 流程分析

  • step0 switch target directory of project and open git-bash
  • step1 git chekout [targetBranchName]
  • step2 git pull
  • step3 [git command about statistic(as follows:↓)]

3 Git代码统计命令

git command about statistic

  • 根据:当前分支+作者+时间段
-- 统计 2024-08-30 当日的指定作者的代码量 (2025-08 亲测)
git log --author=xxx --since=2024-08-30 --until=2025-04-31 --pretty=tformat: --numstat  | grep "\(.html\|.java\|.xml\|.properties\)$" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

git log --author=johnny --since=2020-05-11 --until=2020-06-03 --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | grep "\(.html\|.java\|.xml\|.properties\)$" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

结果:当前分支下,新增代码行数、移除代码行数、总计代码行数(新增-移除)

(可见,不同分支下其统计结果不同)
  • 根据:当前分支+作者
git log --author="johnny" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -;
  • 根据:当前分支(整个项目)
git log  --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }';
(可见,不同分支下其统计结果不同)

4 参考文献

posted @ 2020-06-08 10:57  千千寰宇  阅读(9209)  评论(0)    收藏  举报