我可以让 'git diff' 只显示更改后的文件名和行号吗?

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

Can I make 'git diff' only show the changed file names and line numbers?

gitgit-diff

提问by wei

Basically, I don't want to see the changed content, just the file names and line numbers.

基本上,我不想看到更改的内容,只想看到文件名和行号。

采纳答案by torek

Note: if you're just looking for the namesof changed files (withoutthe line numbersfor lines that were changed), that's easy, click this link to another answer here.

注意:如果你只是寻找的名称(变更文件没有行号已更改为线),这很容易,请点击此链接到这里另一个答案



There's no built-in option for this (and I don't think it's all that useful either), but it ispossible to do this in git, with the help of an "external diff" script.

有没有内置的选项,这个(我不认为这一切都让无论是有用的),但它有可能做到这一点的混帐,与“外部差异”脚本的帮助。

Here's a pretty crappy one; it will be up to you to fix up the output the way you would like it.

这是一个非常蹩脚的;您可以按照自己的意愿修复输出。

#! /bin/sh
#
# run this with:
#    GIT_EXTERNAL_DIFF=<name of script> git diff ...
#
case $# in
1) "unmerged file $@, can't show you line numbers"; exit 1;;
7) ;;
*) echo "I don't know what to do, help!"; exit 1;;
esac

path=
old_file=
old_hex=
old_mode=
new_file=
new_hex=
new_mode=

printf '%s: ' $path
diff $old_file $new_file | grep -v '^[<>-]'

For details on "external diff" see the description of GIT_EXTERNAL_DIFFin the git manual page(around line 700, pretty close to the end).

有关“外部差异”详情,请参阅的说明GIT_EXTERNAL_DIFFgit的手册页(左右线700,相当接近结束)。

回答by phreakhead

So easy:

太简单:

git diff --name-only

Go forth and diff!

去差异化!

回答by ThiefMaster

Line numbers as in number of changed lines or the actual line numbers containing the changes? If you want the number of changed lines, use git diff --stat. This gives you a display like this:

行号是更改的行数还是包含更改的实际行号?如果您想要更改的行数,请使用git diff --stat. 这会给你一个这样的显示:

[me@somehost:~/newsite:master]> git diff --stat
 whatever/views/gallery.py |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

There is no option to get the line numbers of the changes themselves.

没有选项可以获取更改本身的行号。

回答by Seitbekir Seidametov

git diff master --compact-summary

git diff master --compact-summary

Output is:

输出是:

 src/app/components/common/sidebar/toolbar/toolbar.component.html   |  2 +-
 src/app/components/common/sidebar/toolbar/toolbar.component.scss   |  2 --

This is exactly what you need. Same format as when you making commit or pulling new commits from remote.

这正是您所需要的。与从远程提交或拉取新提交时的格式相同。

PS: That's wired that nobody answered this way.

PS:这是有线的,没有人以这种方式回答。

回答by Feiteira

1)My favorite:

1)我的最爱:

git diff --name-status

Prepends file status, e.g.:

前置文件状态,例如:

A   new_file.txt
M   modified_file.txt 
D   deleted_file.txt

2)If you want statistics, then:

2)如果你想要统计,那么:

git diff --stat

will show something like:

将显示如下内容:

new_file.txt         |  50 +
modified_file.txt    | 100 +-
deleted_file         |  40 -

3)Finally, if you really want only the filenames:

3)最后,如果你真的只想要文件名:

git diff --name-only

Will simply show:

将简单地显示:

new_file.txt
modified_file.txt
deleted_file

回答by masterxilo

Shows the file names and amount/nubmer of lines that changed in each file between now and the specified commit:

显示从现在到指定提交之间每个文件中更改的文件名和行数/数量:

git diff --stat <commit-hash>

回答by Ger O'Donnell

I know this is an old question but on Windows, this filters the git output to the files and changed line numbers:

我知道这是一个老问题,但在 Windows 上,这会将 git 输出过滤到文件并更改行号:

(git diff -p --stat) | findstr "@@ --git"

(git diff -p --stat) | findstr "@@ --git"

diff --git a/dir1/dir2/file.cpp b/dir1/dir2/file.cpp
@@ -47,6 +47,7 @@ <some function name>
@@ -97,7 +98,7 @@ <another functon name>

To extract the files and the changed lines from that is a bit more work:

要从中提取文件和更改的行,需要做更多的工作:

for /f "tokens=3,4* delims=-+ " %f in ('^(git diff -p --stat .^) ^| findstr ^"@@ --git^"') do @echo %f

for /f "tokens=3,4* delims=-+ " %f in ('^(git diff -p --stat .^) ^| findstr ^"@@ --git^"') do @echo %f

a/dir1/dir2/file.cpp
47,7
98,7

回答by RedFred

The cleanest output, i.e. file names/paths only, comes with

最干净的输出,即仅文件名/路径,附带

git diff-tree --no-commit-id --name-only -r

HTH

HTH

回答by Edgar Lee

On git version 2.17.1, there isn't a built-in flagto achieve this purpose.

在 上git version 2.17.1没有内置标志来实现此目的。

Here's an example command to filter out the filename and line numbers from an unified diff:

这是从统一差异中过滤出文件名和行号的示例命令:

git diff --unified=0 | grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? @@@)' | paste -s -d':'

For example, the unified diff:

例如,统一的差异:

$ git diff --unified=0
diff --cc foobar
index b436f31,df63c58..0000000
--- a/foobar
+++ b/foobar
@@@ -1,2 -1,2 +1,6 @@@ Line abov
++<<<<<<< HEAD
 +bar
++=======
+ foo
++>>>>>>> Commit message

Will result in:

会导致:

? git diff --unified=0 | grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? @@@)' | paste -s -d':'
foobar:1

To match the output of commands in common grep match results:

要匹配普通 grep 匹配结果中的命令输出:

$ git diff --unified=0 | grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? )| @@@.*' | sed -e '0~3{s/ @@@[ ]\?//}' | sed '2~3 s/$/\n1/g' | sed "N;N;N;s/\n/:/g"
foobar:1:1:Line abov
  1. grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? ): Match filename from diff --cc <filename>OR Match line number from @@@ <from-file-range> <from-file-range> <to-file-range>OR Match remaining text after @@@.
  2. sed -e '0~3{s/ @@@[ ]\?//}': Remove @@@[ ]\?from every 3rd line to get the optional 1 line context before ++<<<<<<< HEAD.
  3. sed '2~3 s/$/\n1/g': Add \n1every 3 lines between the 2nd and 3rd line for the column number.
  4. sed "N;N;N;s/\n/:/g": Join every 3 lines with a :.
  1. grep -Po '^diff --cc \K.*|^@@@( -[0-9]+,[0-9]+){2} \+\K[0-9]+(?=(,[0-9]+)? ): 匹配文件名来自diff --cc <filename>OR 匹配行号来自@@@ <from-file-range> <from-file-range> <to-file-range>OR 匹配之后的剩余文本@@@
  2. sed -e '0~3{s/ @@@[ ]\?//}':@@@[ ]\?从每 3rd 行中删除以获得之前的可选 1 行上下文++<<<<<<< HEAD
  3. sed '2~3 s/$/\n1/g':\n1在第 2 行和第 3 行之间添加每 3 行作为列号。
  4. sed "N;N;N;s/\n/:/g": 每 3 行加入一个:.

回答by plhn

I use grepas a naive solution.

grep用作一个幼稚的解决方案。

$ git diff | grep -A2 -- '---'

an output example:

输出示例:

--- a/fileA.txt
+++ b/fileA.txt
@@ -0,0 +1,132 @@
--
--- a/B/fileC.txt
+++ b/B/fileC.txt
@@ -33663,3 +33663,68800 @@ word_38077.png,Latin
--
--- a/D/fileE.txt
+++ b/D/fileE.txt
@@ -17998,3 +17998,84465 @@ word_23979.png,Latin
--
--- a/F
+++ b/F
@@ -1 +1 @@

Maybe you can see a colored output. It helps you to read outputs easily.

也许您可以看到彩色输出。它可以帮助您轻松读取输出。