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


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


当前回答

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

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

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

其他回答

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

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

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

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:奇怪的是没有人这样回答。

注意:如果您只是寻找更改的文件的名称(没有更改的行号),请在这里查看另一个答案。


这方面没有内置选项(我也不认为它有多大用处),但是在Git中,借助“外部diff”脚本可以做到这一点。

这是一个相当糟糕的问题;这将由您来修复输出您想要的方式。

#! /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=$1
old_file=$2
old_hex=$3
old_mode=$4
new_file=$5
new_hex=$6
new_mode=$7

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

关于“external diff”的详细信息,请参见Git手册中GIT_EXTERNAL_DIFF的描述(大约在700行,非常接近结尾)。

它并不漂亮,但这里有一句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

Use:

git diff --name-only

往前走!