To search for commit content (ie, actual lines of source, as opposed to commit messages and the like), you need to do:
(要搜索提交内容 (即,实际的源代码行,而不是提交消息等),您需要执行以下操作:)
git grep <regexp> $(git rev-list --all)
git rev-list --all | xargs git grep <expression>
git rev-list --all | xargs git grep <expression>
will work if you run into an "Argument list too long" error.
(如果遇到“参数列表过长”错误,则git rev-list --all | xargs git grep <expression>
将起作用。)
If you want to limit the search to some subtree (for instance, "lib/util"), you will need to pass that to the rev-list
subcommand and grep
as well:
(如果要将搜索限制为某些子树(例如“ lib / util”),则需要将其传递给rev-list
子命令和grep
:)
git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util
This will grep through all your commit text for regexp
.
(这将遍历regexp
所有提交文本。)
The reason for passing the path in both commands is because rev-list
will return the revisions list where all the changes to lib/util
happened, but also you need to pass to grep
so that it will only search in lib/util
.
(在两个命令中都传递路径的原因是因为rev-list
将返回所有对lib/util
进行更改的修订列表,但是您还需要传递给grep
以便它仅在lib/util
搜索。)
Just imagine the following scenario: grep
might find the same <regexp>
on other files which are contained in the same revision returned by rev-list
(even if there was no change to that file on that revision).
(试想一下以下情形: grep
可能在rev-list
返回的同一修订版中包含的其他文件上找到相同的<regexp>
(即使该修订版上的文件没有更改)。)
Here are some other useful ways of searching your source:
(以下是一些其他有用的搜索来源的方法:)
Search working tree for text matching regular expression regexp:
(在工作树中搜索与正则表达式regexp匹配的文本:)
git grep <regexp>
Search working tree for lines of text matching regular expression regexp1 or regexp2:
(在工作树中搜索与正则表达式regexp1或regexp2匹配的文本行:)
git grep -e <regexp1> [--or] -e <regexp2>
Search working tree for lines of text matching regular expression regexp1 and regexp2, reporting file paths only:
(在工作树中搜索与正则表达式regexp1和regexp2匹配的文本行,仅报告文件路径:)
git grep -e <regexp1> --and -e <regexp2>
Search working tree for files that have lines of text matching regular expression regexp1 and lines of text matching regular expression regexp2:
(在工作树中搜索具有与正则表达式regexp1匹配的文本行和与正则表达式regexp2匹配的文本行的文件:)
git grep -l --all-match -e <regexp1> -e <regexp2>
Search working tree for changed lines of text matching pattern:
(在工作树中搜索文本匹配模式的更改行:)
git diff --unified=0 | grep <pattern>
Search all revisions for text matching regular expression regexp:
(搜索所有修订以查找与正则表达式regexp匹配的文本:)
git grep <regexp> $(git rev-list --all)
Search all revisions between rev1 and rev2 for text matching regular expression regexp:
(在rev1和rev2之间的所有修订中搜索与正则表达式regexp匹配的文本:)
git grep <regexp> $(git rev-list <rev1>..<rev2>)