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

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

问题描述

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

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).

推荐答案

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

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

$ git log --grep=word

如果您要查找所有提交,其中"word"是在文件内容中添加或删除的(更确切地说:单词"出现的次数已更改),即搜索 commit内容,请使用所谓的"pickaxe"搜索

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

在现代Git中也有

$ git log -Gword

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

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

请注意,默认情况下 -G 接受正则表达式,而 -S 则接受字符串,但是可以使用-pickaxe对其进行修改以接受正则表达式-regex .

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.

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

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);

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

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).


在Git 2.25.1(2020年2月)中,这些正则表达式的文档得到了澄清.


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

请参见提交9299f84 (2020年2月6日)通过 Junio C Hamano合并- gitster -提交0d11410 ,2020年2月12日)

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

diff-options.txt :避免"; regex"示例中的重载\

报告人:亚当·丁伍德迪
签名人:MartinÅgren
评论人:泰勒·布劳

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

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.

该示例是正确的,但是我们可以通过避免编写"regex.*"来使其更容易解开.除非确实有必要提出我们的观点.

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.

git diff 文档现在包括:

The git diff documentation now includes:

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

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);

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

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天全站免登陆