It seems git grep
doesn't have any option to search only in working directory modified files prior to indexing those files. is there any native git command for this purpose or should I use a combo git/linux commands?
Asked
Active
Viewed 1,381 times
2 Answers
14
by using linux grep
and git ls-files
:
$ grep -s "search pattern" $(git ls-files -m)
note 1: grep's -s
option is provided for Suppress error messages about nonexistent or unreadable files
because git ls-files -m
lists deleted files too which causes grep
give "No such file or directory"
error when encounters a nonexistent file.
note 2: git ls-files' -m
option is for listing only modified
files(which also lists removed files too!)

sepehr
- 5,479
- 3
- 29
- 33
-
2This searches the entire contents of modified files. To search only the _changed lines_, see here: https://stackoverflow.com/questions/13955390/git-grep-but-restricted-to-new-or-modified-files-in-the-index – Roy Tinker Feb 05 '18 at 22:26
-
for fish users: grep -s "search pattern" (git ls-files -m) Just omit the dollar! – TheKitMurkit Mar 27 '18 at 20:08
0
git grep
is modelled on plain grep
, perhaps you should just use that.

vonbrand
- 11,412
- 8
- 32
- 52
-
1I prefer to use git build-in grep because of performance boost git-grep gives over grep, but if you know commands to get list of modified files and pass it to linux's grep I take that as an answer. – sepehr Mar 23 '13 at 23:24
-
-
1it seems you didn't see my answer which use your suggested "git ls-files" command. – sepehr Mar 24 '13 at 09:22