这个问题需要“行号”。如果您不关心输出中的行号,请参阅此问题和答案。


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


当前回答

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

git diff --stat <commit-hash>

其他回答

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

git diff --stat <commit-hash>

Use:

git diff master --compact-summary

输出结果为:

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

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

PS:奇怪的是没有人这样回答。

它并不漂亮,但这里有一句bash:

git diff --unified=0 HEAD~1..HEAD | grep -Po '(^diff --git [a-zA-Z/._]*|^@@.*@@)' | while read l; do if [[ -n ${l##@@*} ]]; then f=${l#*/}; else echo "$f:${l##@@ }" | cut -d' ' -f1 | tr -d '-'; fi; done

解释:

git diff——统一=0头~1..头

从Git中检索提交信息

grep -Po '(^diff——git [a-zA-Z/._]*|^@@.*@@)'

建立在先前的答案上,并筛选到包含 文件名和行号

为了提高可读性,将一行代码扩展为多行代码:

while read line; do
  if [[ -n ${line##@@*} ]]; then
    # Grabs filename from this pattern: "diff --git a/....."
    filename=${line#*/};
  else
    # Grabs line number from this patterns: "@@ -<line> +<line> @@"
    echo"$filename:${line##@@ }" | cut -d' ' -f1 | tr -d '-';
  fi;
done

转换为预期输出的字符串解析:

file/name1.txt:34
file/name2.txt:98
file/name2.txt:101
file/name3.txt:2

在git 2.17.1版本中,没有内置标志来实现这一目的。

下面是一个从统一的diff中过滤出文件名和行号的示例命令:

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

例如,统一的差异:

$ 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

会导致:

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

查询普通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

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 @@@. sed -e '0~3{s/ @@@[ ]\?//}': Remove @@@[ ]\? from every 3rd line to get the optional 1 line context before ++<<<<<<< HEAD. sed '2~3 s/$/\n1/g': Add \n1 every 3 lines between the 2nd and 3rd line for the column number. sed "N;N;N;s/\n/:/g": Join every 3 lines with a :.

我最喜欢的:

git diff --name-status

前置文件状态,例如:

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

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

git diff --stat

将显示如下内容:

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

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

git diff --name-only

将简单地显示:

new_file.txt
modified_file.txt
deleted_file