我如何计算git中两次提交之间改变的行数? [英] How can I calculate the number of lines changed between two commits in git?

查看:951
本文介绍了我如何计算git中两次提交之间改变的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以计算git中两次提交之间改变的行数?我知道我可以做一个 git diff ,然后计算这些行,但这看起来很单调。我还想知道我该如何做到这一点,包括我自己在linecounts中的提交。

解决方案

- stat 选项 git diff ,或者如果您想要在脚本中解析它, - numstat 选项。

  git diff --stat< commit-ish> ; <提交肥胖型> 

- stat 产生人类可读的合并后您习惯看到的输出; - numstat 产生了一个很好的表格布局,脚本可以很容易地解释。



我不知何故错过了你期望的在同一时间对多个提交执行此操作 - 这是 git log 的任务。 Ron DeVera谈到这一点,但实际上你可以做的比他所提到的要多得多。由于 git log 在内部调用差异机制以打印请求的信息,所以可以给它任何差异统计选项 - 而不仅仅是 - shortstat 。你可能想要使用的是:

  git log --author =您的名字--stat< commit1> ;. < commit2> 

但您可以使用 - numstat - shortstat git log 也可以用各种其他方式选择提交 - 查看文档。你可能对之类的东西感兴趣,因为(而不是指定提交范围,只选择上周以来的提交)和 - no-merges (合并提交实际上不会引入更改),以及漂亮的输出选项( - pretty = oneline,short,medium,full ...

这是一个单线程来获得全部更改,而不是git log中的每次提交更改(根据需要更改提交选择选项 - 这是提交你从commit1到commit2):

  git log --numstat --pretty =%H--author =你的名字commit1..commit2 | awk'NF == 3 {plus + = $ 1;减去+ = $ 2} END {printf(+%d, - %d \ n,plus,minus)}'

(你必须让git log打印一些关于提交的标识信息;我随意选择了散列,然后使用awk仅挑选出带有三个字段的行,这些字段与stat信息一致)


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.

解决方案

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)

这篇关于我如何计算git中两次提交之间改变的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆