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


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


当前回答

最干净的输出,也就是文件名/路径

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

其他回答

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

git diff --stat <commit-hash>

最干净的输出,也就是文件名/路径

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

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

在Windows上,这将过滤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>

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

/ f”令牌= 3,4 * delims = - +”% f(“^ (git diff - p - stat。^)^ |中^“@@——git ^”)做@echo % f

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

Use:

git diff --name-only

往前走!