git grep——缓存

git grep——缓存,git,grep,Git,Grep,我是否误解了git grep--cached foo的工作原理?运行git版本1.6.4.4或1.6.5.2,git grep--cached foo返回的内容与git grep foo返回的内容完全相同 我认为它的工作原理与git diff--cached类似,只搜索临时区域中的更改。这确实让我相信: 上面的命令提供了我想要的内容的一半,但它丢失了更改出现在哪个文件中的上下文 更新 对于--cached正在查看的内容,我似乎有一个概念错误。它看起来像是在搜索树的状态,假设应用了暂存区域。现在我

我是否误解了git grep--cached foo的工作原理?运行git版本1.6.4.4或1.6.5.2,
git grep--cached foo
返回的内容与
git grep foo
返回的内容完全相同

我认为它的工作原理与git diff--cached类似,只搜索临时区域中的更改。这确实让我相信:

上面的命令提供了我想要的内容的一半,但它丢失了更改出现在哪个文件中的上下文

更新

对于
--cached
正在查看的内容,我似乎有一个概念错误。它看起来像是在搜索树的状态,假设应用了暂存区域。现在我想起来,这是有道理的。我想搜索的是差异,而不是完整的树

具体来说,我想知道我将要提交
SpecialLog(…)
的所有文件的列表(我不关心行号或上下文),这样我就可以编辑这些文件并删除
SpecialLog
。因此,是的,我可以只做
git diff--cached
并在寻呼机中搜索
SpecialLog
,但是对于单个文件中的巨大更改,有很多重复项,并且不清楚我在查看哪个文件

$ git init
Initialized empty Git repository in /tmp/foo/.git/
$ echo hi there >file
$ git add file
$ git commit -m 'added file'
[master (root-commit) dc08993] added file
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file
$ echo hi again >>file
$ git grep again
file:hi again
$ git grep --cached again
$
时光流逝

$ git add file
$ git grep --cached again
file:hi again
要将搜索范围限制为下一次提交的内容,
git diff
通过管道将其输出传输到
$PAGER
。假设您已将寻呼机设置为
less
git diff--cached
在上下文中显示搜索匹配项

在索引中搜索包含提及特殊字符串的更改的文件,如以下示例所示:

$ echo SpecialLog >file2
$ git add file2
$ git diff-index --cached --name-only HEAD
file
file2
$ git diff-index --cached -SSpecialLog --name-only HEAD
file2
$ git diff --cached -SSpecialLog --name-only
file2

谢谢您帮助我理解了我的概念错误(请参阅描述中的更新)。所以我真正想要的是结合git diff和grep的东西。寻呼机工作正常,但这不是我要找的;)好极了!那正是我想要的!
$ git add file
$ git grep --cached again
file:hi again
$ echo SpecialLog >file2
$ git add file2
$ git diff-index --cached --name-only HEAD
file
file2
$ git diff-index --cached -SSpecialLog --name-only HEAD
file2
$ git diff --cached -SSpecialLog --name-only
file2