如何计算 git 中两次提交之间更改的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2528111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:08:55  来源:igfitidea点击:

How can I calculate the number of lines changed between two commits in git?

git

提问by Mike

Is there any easy way to calculate the number of lines changed between two commits in git?

有没有什么简单的方法可以计算 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,并计算行数,但这似乎很乏味。我还想知道如何做到这一点,仅包括我自己在行数中的提交。

回答by Cascabel

You want the --statoption of git diff, or if you're looking to parse this in a script, the --numstatoption.

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

git diff --stat <commit-ish> <commit-ish>

--statproduces the human-readable output you're used to seeing after merges; --numstatproduces a nice table layout that scripts can easily interpret.

--stat产生你在合并后习惯看到的人类可读的输出;--numstat生成脚本可以轻松解释的漂亮表格布局。

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 loginternally 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. Ron DeVera 谈到了这一点,但实际上你可以做的比他提到的要多得多。由于git log内部调用 diff 机制以打印请求的信息,因此您可以为其提供任何 diff stat 选项 - 而不仅仅是--shortstat. 您可能想要使用的是:

git log --author="Your name" --stat <commit1>..<commit2>

but you can use --numstator --shortstatas well. git logcan 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...).

但您也可以使用--numstat--shortstatgit log还可以通过各种其他方式选择提交 - 查看文档。您可能对诸如--since(而不是指定提交范围,只需选择自上周以来的提交)和--no-merges(合并提交实际上不会引入更改)以及漂亮的输出选项 ( --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 获取总更改而不是每次提交更改的单行代码(根据需要更改提交选择选项 - 这是您的提交,从 commit1 到 commit2):

git log --numstat --pretty="%H" --author="Your Name" commit1..commit2 | awk 'NF==3 {plus+=; minus+=} 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 log打印一些关于提交的识别信息;我随意选择了hash,然后用awk只挑出有三个字段的行,也就是有stat信息的行)

回答by Thomas

For the lazy, use git log --stat.

对于懒惰的人,请使用git log --stat.

回答by orkoden

git diff --shortstat

gives you just the number of lines changed and added. This only works with unstaged changes. To compare against a branch:

只为您提供更改和添加的行数。这仅适用于未分阶段的更改。要与分支进行比较:

git diff --shortstat some-branch

回答by Matthew Flaschen

git diff --stat commit1 commit2

EDIT: You have to specify the commits as well (without parameters it compares the working directory against the index). E.g.

编辑:您还必须指定提交(没有参数,它将工作目录与索引进行比较)。例如

git diff --stat HEAD^ HEAD

to compare the parent of HEADwith HEAD.

将 的父级HEAD与进行比较HEAD

回答by Ron DeVera

Assuming that you want to compare all of your commits between abcd123 (the first commit) and wxyz789 (the last commit), inclusive:

假设您想比较 abcd123(第一次提交)和 wxyz789(最后一次提交)之间的所有提交,包括:

git log wxyz789^..abcd123 --oneline --shortstat --author="Mike Surname"

This gives succinct output like:

这给出了简洁的输出,如:

abcd123 Made things better
 3 files changed, 14 insertions(+), 159 deletions(-)
wxyz789 Made things more betterer
 26 files changed, 53 insertions(+), 58 deletions(-)

回答by nmtri

Another way to get all change log in a specified period of time

另一种获取指定时间段内所有更改日志的方法

git log --author="Tri Nguyen" --oneline --shortstat --before="2017-03-20" --after="2017-03-10"

Output:

输出:

2637cc736 Revert changed code
 1 file changed, 5 insertions(+), 5 deletions(-)
ba8d29402 Fix review
 2 files changed, 4 insertions(+), 11 deletions(-)

With a long output content, you can export to file for more readable

输出内容较长,您可以导出到文件以提高可读性

git log --author="Tri Nguyen" --oneline --shortstat --before="2017-03-20" --after="2017-03-10" > /mnt/MyChangeLog.txt

回答by nmtri

Though all above answers are correct, below one is handy to use if you need count of last many commits

尽管以上所有答案都是正确的,但如果您需要对最后多次提交进行计数,则可以使用下面的答案

below one is to get count of last 5 commits

下面是获取最后 5 次提交的计数

git diff $(git log -5 --pretty=format:"%h" | tail -1) --shortstat

git diff $(git log -5 --pretty=format:"%h" | tail -1) --shortstat

to get count of last 10 commits

获取最近 10 次提交的计数

git diff $(git log -10 --pretty=format:"%h" | tail -1) --shortstat

git diff $(git log -10 --pretty=format:"%h" | tail -1) --shortstat

generic - change N with count of last many commits you need

通用 - 使用您需要的最后多次提交的计数更改 N

git diff $(git log -N --pretty=format:"%h" | tail -1) --shortstat

git diff $(git log -N --pretty=format:"%h" | tail -1) --shortstat

to get count of all commits since start

获取自开始以来所有提交的计数

git diff $(git log --pretty=format:"%h" | tail -1) --shortstat

git diff $(git log --pretty=format:"%h" | tail -1) --shortstat

回答by nibinbhaskar

git log --numstat just gives you only the numbers

git log --numstat 只给你数字

回答by undefined

I just solved this problem for myself, so I'll share what I came up with. Here's the end result:

我刚刚为自己解决了这个问题,所以我将分享我的想法。这是最终结果:

> git summary --since=yesterday
total: 114 file changes, 13800 insertions(+) 638 deletions(-)

The underlying command looks like this:

底层命令如下所示:

git log --numstat --format="" "$@" | awk '{files += 1}{ins += }{del += } END{print "total: "files" files, "ins" insertions(+) "del" deletions(-)"}'

Note the $@in the log command to pass on your arguments such as --author="Brian"or --since=yesterday.

请注意$@log 命令中的 以传递您的参数,例如--author="Brian"--since=yesterday

Escaping the awk to put it into a git alias was messy, so instead, I put it into an executable script on my path (~/bin/git-stat-sum), then used the script in the alias in my .gitconfig:

转义 awk 将其放入 git 别名很麻烦,因此,我将其放入路径 ( ~/bin/git-stat-sum)上的可执行脚本中,然后在我的别名中使用脚本.gitconfig

[alias]
    summary = !git-stat-sum \"$@\"

And it works really well. One last thing to note is that file changesis the number of changes to files, not the number of unique files changed. That's what I was looking for, but it may not be what you expect.

而且效果很好。最后要注意的是file changes文件更改的数量,而不是更改的唯一文件的数量。这就是我一直在寻找的,但它可能不是你所期望的。

Here's another example or two

这是另外一两个例子

git summary --author=brian
git summary master..dev
# combine them as you like
git summary --author=brian master..dev
git summary --all

Really, you should be able to replace any git logcommand with git summary.

真的,您应该能够将任何git log命令替换为git summary.