Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

git - 如何在Git历史记录中grep(搜索)已提交的代码(How to grep (search) committed code in the Git history)

I have deleted a file or some code in a file sometime in the past.

(我过去某个时候已经删除了文件或文件中的某些代码。)

Can I grep in the content (not in the commit messages)?

(我可以在内容中(而不是在提交消息中)grep吗?)

A very poor solution is to grep the log:

(一个非常糟糕的解决方案是grep日志:)

git log -p | grep <pattern>

However, this doesn't return the commit hash straight away.

(但是,这不会立即返回提交哈希。)

I played around with git grep to no avail.

(我和git grep玩了无济于事。)

  ask by Ortwin Gentz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...