如何 grep Git 提交某个单词的差异或内容 [英] How to grep Git commit diffs or contents for a certain word

查看:22
本文介绍了如何 grep Git 提交某个单词的差异或内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Git 代码存储库中,我想列出包含某个单词的所有提交.我试过了

git log -p |grep --context=4 "word";

但它不一定会给我回文件名(除非它与我搜索的词相差不到五行.我也试过

git grep "word";

但它只给我当前文件而不是历史.

如何搜索整个历史记录,以便跟踪特定单词的变化?我打算在我的代码库中搜索出现的单词以追踪更改(在文件历史记录中搜索).

解决方案

如果要查找 提交消息 包含给定单词的所有提交,请使用

$ git log --grep=word

如果你想找到所有提交到word"的地方在文件内容中添加或删除了(更准确地说:单词"出现次数改变的地方),即搜索提交内容,使用所谓的镐"搜索

$ git log -Sword

在现代 Git 中也有

$ git log -Gword

查找添加或删除的行与word"匹配的差异(也提交内容).

请注意,-G 默认接受正则表达式,而 -S 接受字符串,但可以使用 --pickaxe 修改以接受正则表达式-正则表达式.

<块引用>

为了说明-S之间的区别;--pickaxe-regex-G,考虑在同一个文件中具有以下差异的提交:

+ return !regexec(regexp, two->ptr, 1, &regmatch, 0);...- hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);

<块引用>

虽然 git log -G"regexec(regexp" 会显示这个提交,git log -S"regexec(regexp" --pickaxe-regex 不会(因为该字符串的出现次数没有改变).


在 Git 2.25.1(2020 年 2 月)中,文档围绕这些正则表达式进行了澄清.

请参阅 commit 9299f84(2020 年 2 月 6 日),作者 Martin Ågren (``).(由 Junio C Hamano 合并 -- gitster --提交 0d11410,2020 年 2 月 12 日)

<块引用>

diff-options.txt:避免正则表达式"示例中的重载

报告人:Adam Dinwoodie
签字人:Martin Ågren
审核人:Taylor Blau

<块引用>

当我们举例说明 -G-S 之间的区别时(使用 --pickaxe-regex),我们使用一个例子diff 和 git diff 调用涉及regexec"、";regexp"、regmatch"等

<块引用>

这个例子是正确的,但我们可以通过避免编写regex.*"来更容易地解开.除非确实需要说明我们的观点.

<块引用>

改用一些虚构的非正则词.

git diff 文档 现在包括:

<块引用>

为了说明-S之间的区别;--pickaxe-regex-G,考虑在同一个文件中具有以下差异的提交:

<块引用>

+ return frotz(nitfol, two->ptr, 1, 0);

<代码>...- 命中 = frotz(nitfol, mf2.ptr, 1, 0);

<块引用>

虽然 git log -G"frotz(nitfol" 会显示这个提交,git log -S"frotz(nitfol" --pickaxe-regex 不会(因为该字符串的出现次数没有改变).

In a Git code repository I want to list all commits that contain a certain word. I tried this

git log -p | grep --context=4 "word"

but it does not necessarily give me back the filename (unless it's less that five lines away from the word I searched for. I also tried

git grep "word"

but it gives me only present files and not the history.

How do I search the entire history so I can follow changes on a particular word? I intend to search my codebase for occurrences of word to track down changes (search in files history).

解决方案

If you want to find all commits where the commit message contains a given word, use

$ git log --grep=word

If you want to find all commits where "word" was added or removed in the file contents (to be more exact: where the number of occurrences of "word" changed), i.e., search the commit contents, use a so-called 'pickaxe' search with

$ git log -Sword

In modern Git there is also

$ git log -Gword

to look for differences whose added or removed line matches "word" (also commit contents).

Note that -G by default accepts a regex, while -S accepts a string, but it can be modified to accept regexes using the --pickaxe-regex.

To illustrate the difference between -S<regex> --pickaxe-regex and -G<regex>, consider a commit with the following diff in the same file:

+    return !regexec(regexp, two->ptr, 1, &regmatch, 0);
...
-    hit = !regexec(regexp, mf2.ptr, 1, &regmatch, 0);

While git log -G"regexec(regexp" will show this commit, git log -S"regexec(regexp" --pickaxe-regex will not (because the number of occurrences of that string did not change).


With Git 2.25.1 (Feb. 2020), the documentation is clarified around those regexes.

See commit 9299f84 (06 Feb 2020) by Martin Ågren (``). (Merged by Junio C Hamano -- gitster -- in commit 0d11410, 12 Feb 2020)

diff-options.txt: avoid "regex" overload in the example

Reported-by: Adam Dinwoodie
Signed-off-by: Martin Ågren
Reviewed-by: Taylor Blau

When we exemplify the difference between -G and -S (using --pickaxe-regex), we do so using an example diff and git diff invocation involving "regexec", "regexp", "regmatch", etc.

The example is correct, but we can make it easier to untangle by avoiding writing "regex.*" unless it's really needed to make our point.

Use some made-up, non-regexy words instead.

The git diff documentation now includes:

To illustrate the difference between -S<regex> --pickaxe-regex and -G<regex>, consider a commit with the following diff in the same file:

+    return frotz(nitfol, two->ptr, 1, 0);

...
-    hit = frotz(nitfol, mf2.ptr, 1, 0);

While git log -G"frotz(nitfol" will show this commit, git log -S"frotz(nitfol" --pickaxe-regex will not (because the number of occurrences of that string did not change).

这篇关于如何 grep Git 提交某个单词的差异或内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆