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

查看:112
本文介绍了如何在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.

推荐答案

对于commit content (即实际的源代码行,而不是提交消息等),你需要做的是:

To search for commit content (i.e., actual lines of source, as opposed to commit messages and the like), what you need to do is:

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

更新 git rev-list --all | xargs git grep表达式会在你遇到参数列表太长时间错误时工作

Updates: git rev-list --all | xargs git grep expression will work if you run into an "Argument list too long" error

如果你想限制搜索到某个子树(例如lib / util),您需要将它传递给 rev-list 子命令并将 grep 作为< well:

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

将grep通过所有提交文本的正则表达式。

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 on lib/util.

试想下面的情况:<$ c $对于包含在由 rev返回的相同修订中的其他文件,c> grep 可能会找到相同的< regexp> -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:

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

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匹配的文本行的工作树,仅报告文件路径: p>

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

git grep -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>

搜索文本匹配正则表达式正则表达式的所有修订版本:

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