Git grep --cached

I don’t understand how it works git grep --cached foo? Running git version 1.6.4.4 or 1.6.5.2 git grep --cached fooreturns the same as git grep foo.

I thought it would work like git diff --cached, just looking for changes in the staging area. This certainly means that the man page makes me believe:

git diff [--options] --cached [<commit>] [--] [<path>…]

This form is designed to view the changes that you have made for the next commit with respect to the name <commit>. Usually you need to compare with the last commit, so if you don't give it <commit>, it will be by default HEAD. If it HEADdoes not exist (for example, non-native branches) and is <commit>not specified, it shows all the phased changes. --stagedis synonymous with --cached.

Is this just a mistake, or is there an alternative / best way to find the changes you need to make to mention foo?

git diff --cached | grep foo

The above command gives me half of what I want, but it loses the context of the file in which the change appears.

UPDATE

It seems I have a concept error for which it is looking --cached. It looks like he is looking for the state of the tree, suggesting that the staging area is applied. It makes sense now that I think about it. What I want to find is the difference, not the complete tree.

, ( ), SpecialLog(...), SpecialLog. , git diff --cached SpecialLog, , , .

+5
1
$ 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
+9

All Articles