10

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?

sepehr
  • 5,479
  • 3
  • 29
  • 33

2 Answers2

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
  • 2
    This 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
  • 1
    I 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
  • Look at `git ls-files --modified` and such – vonbrand Mar 24 '13 at 00:38
  • 1
    it seems you didn't see my answer which use your suggested "git ls-files" command. – sepehr Mar 24 '13 at 09:22