有没有简单的方法来计算Git中两次提交之间更改的行数?
我知道我可以做一个git diff,并数行,但这似乎很乏味。我还想知道如何做到这一点,在行计数中只包括我自己的提交。
有没有简单的方法来计算Git中两次提交之间更改的行数?
我知道我可以做一个git diff,并数行,但这似乎很乏味。我还想知道如何做到这一点,在行计数中只包括我自己的提交。
当前回答
对于懒惰的人,使用git log——stat。
其他回答
您需要git diff的——stat选项,或者如果您希望在脚本中解析它,则需要——numstat选项。
git diff --stat <commit-ish> <commit-ish>
——stat生成你习惯在合并后看到的人类可读的输出;——numstat生成一个很好的表布局,脚本可以很容易地解释。
我在某种程度上错过了你正在寻找在同一时间多次提交这样做-这是一个任务的git日志。Ron DeVera提到了这一点,但实际上你可以做的比他提到的多得多。因为git日志在内部调用diff机制来打印请求的信息,你可以给它任何diff stat选项——而不仅仅是——shortstat。你可能想用的是:
git log --author="Your name" --stat <commit1>..<commit2>
但你也可以使用——numstat或——shortstat。Git日志还可以以各种其他方式选择提交-请查看文档。您可能会对——since(而不是指定提交范围,只是选择上周以来的提交)和——no-merges(合并提交实际上不引入更改)以及漂亮的输出选项(——pretty=oneline, short, medium, full…)感兴趣。
这是一个一行程序,从git日志中获得所有更改,而不是每次提交的更改(根据需要更改提交选择选项-这是由你提交的,从commit1到commit2):
git log --numstat --pretty="%H" --author="Your Name" commit1..commit2 | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d, -%d\n", plus, minus)}'
(你必须让git日志打印一些关于提交的识别信息;我任意地选择了哈希,然后使用awk只挑出带有三个字段的行,这是带有统计信息的行)
如果你想检查两个分支或提交之间插入、删除和提交的数量。
使用提交id:
git log <commit-id>..<commit-id> --numstat --pretty="%H" --author="<author-name>" | awk 'NF==3 {added+=$1; deleted+=$2} NF==1 {commit++} END {printf("total lines added: +%d\ntotal lines deleted: -%d\ntotal commits: %d\n", added, deleted, commit)}'
使用分支:
git log <parent-branch>..<child-branch> --numstat --pretty="%H" --author="<author-name>" | awk 'NF==3 {added+=$1; deleted+=$2} NF==1 {commit++} END {printf("total lines added: +%d\ntotal lines deleted: -%d\ntotal commits: %d\n", added, deleted, commit)}'
git log --numstat
只给你数字
另一种获取指定时间段内所有更改日志的方法
git log --author="Tri Nguyen" --oneline --shortstat --before="2017-03-20" --after="2017-03-10"
输出:
2637cc736 Revert changed code
1 file changed, 5 insertions(+), 5 deletions(-)
ba8d29402 Fix review
2 files changed, 4 insertions(+), 11 deletions(-)
对于较长的输出内容,您可以将其导出到文件以获得更好的可读性
git log --author="Tri Nguyen" --oneline --shortstat --before="2017-03-20" --after="2017-03-10" > /mnt/MyChangeLog.txt
对于懒惰的人,使用git log——stat。