Git diff:是否可以仅显示更改的行

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

Git diff: is it possible to show ONLY changed lines

gitdiffgit-diff

提问by Artem

I'm trying to get only new version of lines which have changed and not all the other info which git diff shows.

我试图只获取已更改的新版本的行,而不是 git diff 显示的所有其他信息。

For:

为了:

git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix

It shows:

表明:

diff --git file1 file2
index d9db605..a884b50 100644
--- file1
+++ file2
@@ -16 +16 @@ bla bla bla
-old text
+new text

what I want to see is only:

我想看到的只是:

new text

Is it possible?

是否可以?

采纳答案by michas

Only added lines does not make sense in all cases. If you replaced some block of text and you happend to include a single line which was there before, git has to match and guess. - Usually the output of git diffcould be used as input for patchafterwards and is therefore meaningful. Only the added lines are not precisely defined as git has to guess in some cases.

仅添加行在所有情况下都没有意义。如果您替换了一些文本块,并且碰巧包含了一行之前存在的内容,则 git 必须进行匹配和猜测。- 通常的输出git diff可以用作patch之后的输入,因此是有意义的。只有添加的行没有精确定义,因为在某些情况下 git 必须猜测。

If you nevertheless want it, you cannot trust a leading +sign alone. Maybe filtering all the green line is better:

如果你仍然想要它,你不能只相信一个领先的+标志。也许过滤所有绿线更好:

git diff --color=always|perl -wlne 'print  if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'

for only deleted lines filter for all the red lines:

对于所有红线,仅删除线过滤器:

git diff --color=always|perl -wlne 'print  if /^\e\[31m-(.*)\e\[m$/'

to inspect the color codes in the output you could use:

要检查您可以使用的输出中的颜色代码:

git diff --color=always|ruby -wne 'p $_'

回答by metacubed

If you specifically want only the new textpart, then use the following:

如果您只需要该new text部分,请使用以下内容:

git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix | egrep "^\+"

This is basically your code, piped into the egrepcommand with a regex. The regex will filter only lines starting with a plus sign.

这基本上是您的代码,egrep使用正则表达式通过管道传输到命令中。正则表达式将仅过滤以加号开头的行。

回答by Srini

You can use:

您可以使用:

git diff -U0 <commit-hash> | grep "^\+\""

git diff -U0 <commit-hash> | grep "^\+\""

This will give your output as "+new text"

这将使您的输出为“+新文本”

回答by wisbucky

Here is an answer using grep. It retains the original red/green colors for readability. I provided a few variations in syntax:

这是使用grep. 它保留了原始的红色/绿色以提高可读性。我提供了一些语法变化:

git diff --color | grep --color=never $'^\e\[3[12]m'
git diff --color | grep --color=never $'^3\[3[12]m'
git diff --color | grep --color=never -P '^\e\[3[12]m'
git diff --color | grep --color=never -P '^3\[3[12]m'

Explanation:

解释:

  • git diff --coloris needed to prevent git from disabling the color when it is piping.
  • grep --color=neverprevents grep from highlighting the matched string (which removes the original color from the original command)
  • Match lines that start with red (\e[31m) or green (\e[32m) escape codes.
  • The $'...'(ANSI-C quoting syntax) or -P(perl syntax) is to let grepto interpret \eor \033as an ESCcharacter.
  • git diff --color需要防止 git 在管道时禁用颜色。
  • grep --color=never防止 grep 突出显示匹配的字符串(从原始命令中删除原始颜色)
  • 匹配以红色 ( \e[31m) 或绿色 ( \e[32m) 转义码开头的行。
  • $'...'(ANSI-C引用语法)或-P(perl的语法)是让grep解释\e\033作为ESC字符。