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

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

问题描述

在Git代码仓库中,我想列出包含某个单词的所有提交。我试过这个

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

但它并不一定会给我回文件名(除非它远不及我搜索的单词5
行,我也试过了

  git grepword

但它只给出了现在的文件而不是历史记录。



如何搜索整个历史记录,以便跟踪特定单词的变化?我的意思是搜索我的代码库以查找单词的发生情况以追踪更改(在文件历史记录中搜索)。

解决方案

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

  $ git log --grep = word 

如果您想查找所有在文件内容中添加或删除word的提交(更确切地说:word出现次数发生变化) ,即搜索提交内容,使用使用 $ b

  $ git log -Sword 

在现代的git中也有

$ p $ $ git log -Gword

查找差异,其添加或删除的行匹配word(也 )。



请注意,默认情况下 -G 接受一个正则表达式,而 -S 接受一个字符串,但可以修改为使用 - pickaxe-regex 来接受正则表达式。


为了说明 -S< regex> --pickaxe-regex -G< regex> ,请考虑在同一个文件中使用以下差异提交:

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

虽然 git log -Gregexec \(regexp将显示此提交, git log -S -Sregexec(regexp--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 5 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 mean to search my codebase for occurrences of word to track down changes (search in files history).

解决方案

If you want to find all commits where commit message contains 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 number of occurences of "word" changed), i.e. search the commit contents, use 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 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).

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

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