git diff --name-only
开始进行差异比较吧!
git diff --name-only master..HEAD
。 - Hettomei行号是指更改的行数还是包含更改的行号?如果您想获取更改的行数,请使用git diff --stat
。这将给您一个类似于以下的显示:
[me@somehost:~/newsite:master]> git diff --stat
whatever/views/gallery.py | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)
无法获取更改本身的行号选项。
#! /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 '^[<>-]'
有关“外部差异”的详细信息,请参阅Git手册页上GIT_EXTERNAL_DIFF
的描述(大约在第700行左右,靠近末尾)。
| grep -o '^[0-9]*'
可以只得到数字,假设您不关心右侧。 - GKFX--diff-filter=...
,其中 ...
部分是您想要查看的更改类型:M
表示修改,A
表示添加,D
表示删除,其他类型请参考 git diff
文档。 - torek使用:
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 --
这正是你所需要的。与你进行提交或从远程拉取新提交时相同的格式。
附注:很奇怪没有人以这种方式回答。
1) 我最喜欢的:
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
git diff --stat <commit-hash>
(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>
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
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 --unified=0 HEAD~1..HEAD
从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 version 2.17.1
上,没有内置标志 来实现此目的。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
$ 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]+)? )
:匹配diff --cc <filename>
中的文件名,或者匹配@@@ <from-file-range> <from-file-range> <to-file-range>
中的行号,或者匹配@@@
后面的文本。sed -e '0~3{s/ @@@[ ]\?//}'
:从每个第三行开始删除@@@[ ]\?
,以获取++<<<<<<< HEAD
之前的可选1行上下文。sed '2~3 s/$/\n1/g'
:在第2行和第3行之间的每3行添加\n1
,用于列号。sed "N;N;N;s/\n/:/g"
:将每3行连接成一个:
。
git diff master --numstat
或git diff master --compact-summary
。 - undefined