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
- 根據:當前分支+作者+時間段
git log --author=zengtai --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="zengtai" --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 }';

(可見,不同分支下其統計結果不同)