如何grep git diff?

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

How to grep the git diff?

gitgrepdiff

提问by Kuba

Is there a way to show the git-diff filtered by a given pattern.

有没有办法显示按给定模式过滤的 git-diff。

Something like

就像是

git grepdiff pattern

changed file
+++ some sentence with pattern
changed file 2
--- some other pattern

Unfortunately the simplest solution is not good enough

不幸的是,最简单的解决方案还不够好

git diff | grep pattern 

+++ some sentence with pattern
--- some other pattern
# not an option as doesn't put the filename close to the match

I came with a workaround using awk

我提出了一个使用 awk 的解决方法

git diff | awk "/\+\+\+/{f = $2}; /PATTERN/ {print f $0} "

But would love to find out that there is a command for this.

但是很想知道有一个命令。

回答by CharlesB

Not sure but isn't git diff -G <regex>flag OK?

不确定,但不是git diff -G <regex>flag 好吗?

-G < regex>

Look for differences whose added or removed line matches the given <regex>.

-G <正则表达式>

Look for differences whose added or removed line matches the given <regex>.

回答by robinst

Have you tried git diff -S<string>or git diff -G".*string.*"? Note that they are not equivalent, see the documentation about pickaxefor what -S does.

您是否尝试过git diff -S<string>git diff -G".*string.*"?请注意,它们不是等价的,有关-S 的作用,请参阅有关镐的文档

回答by robinst

Another possibility would be to view the whole diff and search the output using the normal lesscommands (type /and then the pattern).

另一种可能性是查看整个差异并使用普通less命令(键入/然后是模式)搜索输出。

When you have lessconfigured to show some lines before the match using --jump-target=N, this is pretty useful. Try it like this:

当您使用less配置为在匹配之前显示一些行时--jump-target=N,这非常有用。像这样尝试:

PAGER="/usr/bin/less --jump-target=10" git diff

This means that the match should be shown on line 10 (shows 9 lines of context above), which may be enough to also see the file name.

这意味着匹配应该显示在第 10 行(上面显示了 9 行上下文),这可能足以看到文件名。

You can also use e.g. --jump-target=.5to make it position the match in the middle of the screen.

您还可以使用 eg--jump-target=.5将匹配项定位在屏幕中间。

回答by chelmertz

I use git log -p, which opens less (configurable, though), which in turn can be searched for with /. There's also git log -S <searchword>.

我使用git log -p,它打开较少(但可配置),反过来可以使用/. 还有git log -S <searchword>

回答by petiar

This did the job for me, I hope it will help someone:

这对我有用,我希望它会帮助某人:

git diff | grep  -P '^\+|^\-'

回答by Hazzard17

I think your approach to "grep" diffoutput is the best workaround.

我认为你的“grep”diff输出方法是最好的解决方法。

You may improve your awk script by using sed:

您可以使用 sed 改进您的 awk 脚本:

colored="(^[\[[0-9;]*[a-zA-Z])"
marker="^$colored+diff"
pattern="^$colored+.*(\+|\-).*PATTERN"
git diff --color | sed -rn -e "/$marker/! H; /$marker/ ba; $ ba; b; :a; x; /$pattern/ p"
  • colored: regex to match terminal colored lines
  • marker: marker to match division from differents diffhunks, lines starting with colored "diff"
  • pattern: pattern to search for, lines starting with colored "+" or "-" and containing "PATTERN"
  • colored: 正则表达式匹配终端彩色线
  • marker: 标记以匹配不同diff帅哥的划分,以彩色“diff”开头的行
  • pattern:要搜索的模式,以彩色“+”或“-”开头并包含“PATTERN”的行

This will print full diff hunks, with added or removed PATTERN, also maintaining useful colored output.

这将打印完整的差异块,添加或删除 PATTERN,同时保持有用的彩色输出。

Note that ^[in coloredshould be actual, literal ^[. You can type them in bash by pressing Ctrl+ V, Ctrl+ [

请注意,^[incolored应该是实际的,文字的^[。您可以通过按Ctrl+ V, Ctrl+在 bash 中键入它们[

回答by Leon

Here is a custom diff tool that allows grepping inside changes (but not the context):

这是一个自定义差异工具,允许在内部更改(但不是上下文):

Usage

用法

GIT_EXTERNAL_DIFF="mydiff --grep foo" git diff

GIT_EXTERNAL_DIFF="mydiff --grep foo" git diff

This will output those lines in your changes that contain foo(including lines where foodisappeared because of your changes). Any grep pattern can be used instead of foo.

这将输出更改中包含的那些行foo(包括foo由于更改而消失的行)。可以使用任何 grep 模式代替foo.

Each output line starts with the following prefix:

每个输出行都以以下前缀开头:

filename: oldlinenum: newlinenum|

The script can also be used without the --grepoption, in which case it simply formats the full diff (i.e. providing full context) as described above.

该脚本也可以在没有--grep选项的情况下使用,在这种情况下,它只是按照上述方式格式化完整的差异(即提供完整的上下文)。

mydiff

我的差异

#!/bin/bash

my_diff()
{
    diff --old-line-format=""':%6dn:      |-%L'     \
         --new-line-format=""':      :%6dn|+%L'     \
         --unchanged-line-format=""':%6dn:%6dn| %L' \
          
}

if [[  == '--grep' ]]
then
    pattern=""
    shift 2
    my_diff "" "" ""|grep --color=never '^[^|]\+|[-+].\+'"$pattern"'.*'
else
    my_diff "" "" ""
fi

exit 0

回答by Horace

On Windows, a simple solution is:

在 Windows 上,一个简单的解决方案是:

git diff -U0 | findstr string

If you want grouping by filename, use this

如果要按文件名分组,请使用此

FOR /F "usebackq delims==" %i IN (`git diff --name-only`) do git diff -U0 %~fi | findstr string