14

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"?

3 Answers 3

20

:/ 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

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`
4
  • still not ideal, since paths won't run from top level, but nice.
    – Chi-Lan
    Jun 3, 2012 at 7: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). Jun 11, 2012 at 12:41
  • The git config man page link is broken. Can you please update it? Feb 22, 2019 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, 2021 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.)

1
  • still not ideal, since paths won't show from current directory, but thanks
    – Chi-Lan
    Jun 3, 2012 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.