Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How grep through your staged files prior to committing?

So prior to running git commit I often will run the following:

git grep --cached -l -I "debugger"

I thought it was similar to:

git diff --cached

(which will show you all the changes you are about to commit, ie. will show you the diff in your staged files).

Unfortunately, I just found that the --cached option for git grep simply tells git to "only" look at everything in its index.

So how can I run git grep and have it only grep through my staged files?

(Yes, I know I could simply do git diff --cached and search in that, but I would rather have the programmatic ability to grep through my staged files.)

like image 306
steve Avatar asked Nov 04 '10 22:11

steve


2 Answers

If you have a Unix-like shell available, the answer is pretty simple:

git grep --cached "debugger" $(git diff --cached --name-only)

This will run git grep on the list of staged files.

like image 88
Markus Avatar answered Oct 19 '22 14:10

Markus


A lot of pre-commit hooks use git diff-index --cached -S<pat> REV to find changes which add or remove a particular pattern. So in your case, git diff-index --cached -Sdebugger HEAD. You may want to add -u to get a diff as well, otherwise it just identifies the offending file.

like image 37
Ben Jackson Avatar answered Oct 19 '22 12:10

Ben Jackson