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

查看:137
本文介绍了如何在Git历史记录中grep(搜索)已提交的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去某个时候已经删除了文件或文件中的某些代码。我可以在内容中添加grep(不在提交消息中)吗?

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:

git log -p | grep <pattern>

但是,这不会立即返回提交哈希。我玩了 git grep 无济于事。

However, this doesn't return the commit hash straight away. I played around with git grep to no avail.

推荐答案

进行搜索对于提交内容(即,实际的源代码行,而不是提交消息等),您需要执行以下操作:

To search for commit content (i.e., 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<表达式> 将起作用。

如果要限制搜索到某个子树(例如 lib / util),则需要将其传递给 rev-list 子命令和 grep

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:

git grep <regexp> $(git rev-list --all -- lib/util) -- lib/util

此将会遍历 regexp 的所有提交文本。

This will grep through all your commit text for regexp.

在两个命令中传递路径的原因是 rev-list 将返回修订列表,其中对 lib / util 的所有更改都已发生,但是您还需要传递给 grep ,以便仅在 lib / util 中搜索。

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.

试想一下以下情况: grep 可能在包含的其他文件上找到相同的® regexp> rev-list 返回的同一修订版中(即使该修订版对该文件没有更改)。

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

以下是搜索来源的其他有用方法:

Here are some other useful ways of searching your source:

在工作树中搜索与正则表达式regexp匹配的文本:

Search working tree for text matching regular expression regexp:

git grep <regexp>

在工作树中搜索与正则表达式regexp1或regexp2匹配的文本行:

Search working tree for lines of text matching regular expression regexp1 or regexp2:

git grep -e <regexp1> [--or] -e <regexp2>

在工作树中搜索与正则表达式regexp1和regexp2匹配的文本行,仅报告文件路径:

Search working tree for lines of text matching regular expression regexp1 and regexp2, reporting file paths only:

git grep -l -e <regexp1> --and -e <regexp2>

在工作树中搜索文本匹配正则表达式regexp1和文本匹配正则表达式的文件regexp2:

Search working tree for files that have lines of text matching regular expression regexp1 and lines of text matching regular expression 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>

在所有修订中搜索与正则表达式regexp匹配的文本:

Search all revisions for text matching regular expression regexp:

git grep <regexp> $(git rev-list --all)

在rev1和rev2之间的所有修订中搜索常规文本表达式正则表达式:

Search all revisions between rev1 and rev2 for text matching regular expression regexp:

git grep <regexp> $(git rev-list <rev1>..<rev2>)

这篇关于如何在Git历史记录中grep(搜索)已提交的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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