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

查看:55
本文介绍了如何在 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 可能会在 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 之间的所有修订版本以查找匹配正则表达式 regexp 的文本:

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