有没有什么简单的方法来计算在git中两次提交之间改变的行数? 我知道我可以做一个git diff ,并计数线,但这似乎是乏味的。 我也想知道我该怎么做,包括我自己在linecounts中的提交。
Is there any easy way to calculate the number of lines changed between two commits in git? I know I can do a git diff, and count the lines, but this seems tedious. I'd also like to know how I can do this, including only my own commits in the linecounts.
最满意答案
你想要git diff的--stat选项,或者如果你想在一个脚本中解析这个,那么--numstat选项。
git diff --stat <commit-ish> <commit-ish>--stat生成您在合并之后看到的人类可读输出; --numstat生成一个很好的表格布局,脚本可以轻松解释。
我以某种方式错过了您同时在多个提交上执行此操作 - 这是git log的任务。 罗恩·德维拉(Ron DeVera)就是这样介绍的,但实际上你可以做的比他提到的还要多。 由于git log内部调用diff机制以打印所请求的信息,所以您可以给它任何diff stat选项 - 不只是--shortstat 。 你可能想要使用的是:
git log --author="Your name" --stat <commit1>..<commit2>但是您也可以使用--numstat或--shortstat 。 git log也可以以各种其他方式选择提交 - 看看文档 。 您可能对--since (而不是指定提交范围,只是从上周选择提交)和--no-merges (合并提交不实际引入更改)以及漂亮的输出选项( --pretty=oneline, short, medium, full... )。
这是一个一线程,可以从git log获取总体更改,而不是按照每次提交更改(根据需要更改提交选择选项) - 这是由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只选出三个字段的行,这些字段是具有统计信息的行)
You want the --stat option of git diff, or if you're looking to parse this in a script, the --numstat option.
git diff --stat <commit-ish> <commit-ish>--stat produces the human-readable output you're used to seeing after merges; --numstat produces a nice table layout that scripts can easily interpret.
I somehow missed that you were looking to do this on multiple commits at the same time - that's a task for git log. Ron DeVera touches on this, but you can actually do a lot more than what he mentions. Since git log internally calls the diff machinery in order to print requested information, you can give it any of the diff stat options - not just --shortstat. What you likely want to use is:
git log --author="Your name" --stat <commit1>..<commit2>but you can use --numstat or --shortstat as well. git log can also select commits in a variety other ways - have a look at the documentation. You might be interested in things like --since (rather than specifying commit ranges, just select commits since last week) and --no-merges (merge commits don't actually introduce changes), as well as the pretty output options (--pretty=oneline, short, medium, full...).
Here's a one-liner to get total changes instead of per-commit changes from git log (change the commit selection options as desired - this is commits by you, from commit1 to 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)}'(you have to let git log print some identifying information about the commit; I arbitrarily chose the hash, then used awk to only pick out the lines with three fields, which are the ones with the stat information)
更多推荐
发布评论