有没有办法添加 git show 行、更改行和删除行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9933325/
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 13:20:08  来源:igfitidea点击:

Is there a way of having git show lines added, lines changed and lines removed?

git

提问by Juan Alonso

"git diff --stat" and "git log --stat" show output like:

"git diff --stat" 和 "git log --stat" 显示如下输出:

$ git diff -C --stat HEAD c9af3e6136e8aec1f79368c2a6164e56bf7a7e07
 app/controllers/application_controller.rb |   34 +++-------------------------
 1 files changed, 4 insertions(+), 30 deletions(-)

But what really happened in that commit was that 4 lines were changed and 26 lines were deleted which is different than adding 4 lines and deleting 30.

但在那个提交中真正发生的是 4 行被更改并删除了 26 行,这与添加 4 行和删除 30 行不同。

Is there any way of getting the delta LOCs (26 in this case)? I don't really care about differentiating between lines added or removed.

有没有办法获得增量 LOC(在这种情况下为 26)?我真的不关心区分添加或删除的行。

回答by quornian

You can use:

您可以使用:

git diff --numstat

to get numerical diff information.

获取数字差异信息。

As far as separating modification from an add and remove pair, --word-diffmight help. You could try something like this:

至于将修改与添加和删除对分开,--word-diff可能会有所帮助。你可以尝试这样的事情:

MOD_PATTERN='^.+(\[-|\{\+).*$' \
ADD_PATTERN='^\{\+.*\+\}$' \
REM_PATTERN='^\[-.*-\]$' \
git diff --word-diff --unified=0 | sed -nr \
    -e "s/$MOD_PATTERN/modified/p" \
    -e "s/$ADD_PATTERN/added/p" \
    -e "s/$REM_PATTERN/removed/p" \
    | sort | uniq -c

It's a little long-winded so you may want to parse it in your own script instead.

它有点冗长,因此您可能希望在自己的脚本中解析它。

回答by yhluo

  1. If you want to know the lines added/changed/deleted by a commit with id commit-id, you could use

    git show commit-id --stat
    

    or

    git diff commit-id-before commit-id --stat
    
  2. If you wat to know the lines added/changed/deleted by a range commits, you could use

    git diff commit-id1 commit-id2 --stat
    
  3. If you want to know the lines added/changed/deleted by each commit, you could use

    git log --stat
    
  1. 如果您想知道通过 id 提交添加/更改/删除的行commit-id,您可以使用

    git show commit-id --stat
    

    或者

    git diff commit-id-before commit-id --stat
    
  2. 如果您想知道范围提交添加/更改/删除的行,您可以使用

    git diff commit-id1 commit-id2 --stat
    
  3. 如果您想知道每次提交添加/更改/删除的行,您可以使用

    git log --stat
    

回答by Falieson

If all of your files are staged for commit, to see the --numstatgo like this:

如果您的所有文件都已暂存以进行提交,请查看--numstat如下代码:

git diff --numstat HEAD~

回答by Daniel Pittman

git uses "unified" diff, which only has added and deleted lines, as the diff format. You have to do something external to get a diff that shows add, delete, and change information.

git 使用“统一”差异,它只添加和删除了行,作为差异格式。您必须执行一些外部操作才能获得显示添加、删除和更改信息的差异。

https://wiki.postgresql.org/wiki/Working_with_Git#Context_diffs_with_Gitgives links to a script that allows running regular old "diff" - and from that you can generate a "context" diffoutput. Context diff does show added, removed, and changed lines, which should allow you to get the data you want.

https://wiki.postgresql.org/wiki/Working_with_Git#Context_diffs_with_Git提供了一个脚本的链接,该脚本允许运行常规的旧“差异”——您可以从中生成“上下文”差异输出。上下文差异确实显示添加、删除和更改的行,这应该允许您获取所需的数据。

回答by Flux

You could use diffstatto show the number of modified lines. For example:

您可以使用diffstat来显示修改后的行数。例如:

git diff HEAD c9af3e6136e8aec1f79368c2a6164e56bf7a7e07 | diffstat -C -m

The -Coption is for getting colourised output; the -moption is for showing the number of modified lines. Sample output:

-C选项用于获得彩色输出;该-m选项用于显示修改的行数。示例输出:

 app/controllers/application_controller.rb |   30 -------------------!!!
 1 files changed, 0 insertions(+), 26 deletions(-), 4 modifications(!)

The count for the number of lines modified is approximate, as man diffstatsays:

修改的行数是近似值,如下所示man diffstat

-mmerge insert/delete counts from each "chunk" of the patch file to approximate a count of the modified lines.

-m从补丁文件的每个“块”合并插入/删除计数,以近似修改行的计数。

One major difference between git diff --statand diffstat: diffstatdoes not show file moves/renames (e.g. app/{a.rb => b.rb}).

git diff --statdiffstat:之间的主要区别之一diffstat是不显示文件移动/重命名(例如app/{a.rb => b.rb})。