13

Normally when I issue git grep, it will only search the current directory and below, for instance

$ cat A
1
$ cd d
$ cat B
1
$ git grep 1
B:1
$ cd ..;git grep 1
A:1
B:1

How can I tell git grep "search the entire tree, no matter the current working directory I'm in"?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Chi-Lan
  • 3,575
  • 3
  • 22
  • 24

3 Answers3

16

:/ magic path

git grep pattern -- :/

works on Git 1.9.1 for this and other commands, as mentioned at: https://stackoverflow.com/a/31696782/895245

TODO I can't find a clear documentation reference after grepping the docs, but it is mentioned some times in the release notes at:

Documentation/RelNotes/1.7.6.txt:39: be a no-op, but "git add -u :/" would add the updated contents of

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
11

Git aliases that run shell commands are always executed in the top level directory (see the git config man page), so you can add this to your .gitconfig file:

[alias]
    rgrep = !git grep

Alternatively, you could use git rev-parse --show-toplevel to get the root directory, which you could then pass to git grep as the basis of a script or alias:

git grep $pattern -- `git rev-parse --show-toplevel`
pn11
  • 7
  • 3
georgebrock
  • 28,393
  • 13
  • 77
  • 72
  • still not ideal, since paths won't run from top level, but nice. – Chi-Lan Jun 03 '12 at 07:35
  • @Chi-Lan: Looking at this again, I don't see any problem with paths and a `!git grep` alias. The whole command (including the arguments you pass to it) is executed in the repository's root directly, so the paths should be relative to the repository root too. My final update was no different to this solution (so I've removed it). – georgebrock Jun 11 '12 at 12:41
  • The git config man page link is broken. Can you please update it? – Kamaraju Kusumanchi Feb 22 '19 at 23:32
  • It works but it does not show the find files full path from the root directory, would it be possible also show the find file's full path from git root path? – alper Jul 31 '21 at 13:49
0

Add this to your Git config file to make a grepall alias that will do what you want:

[alias]
    grepall = !git grep

(Non-native aliases, e.g. those that begin with a !, are always run from the top level of the repository.)

Amber
  • 507,862
  • 82
  • 626
  • 550