2

I'm attempting to find files in my git repository using git grep and I have no easy way of doing so without manual searching. I have found one workaround like this:

git grep -l 'term1' | xargs -i grep -l term2 {}

But I'm wondering if there is a way similar to this which doesn't require xargs:

git grep -l -E 'term1|term2'

This essentially means show me the files containing either term1 or terms2... is there a "show me files with BOTH these terms."

I'm trying to use the git grep command in a way that is not practical for piping commands into other commands. I really hate working with python's subprocess module and its use of piping...

JacobIRR
  • 8,545
  • 8
  • 39
  • 68

2 Answers2

2

Two ways to achieve the goal:

1) combining multiple patterns specified by -e option with --and flag

--and
--or
--not
( …​ )
Specify how multiple patterns are combined using Boolean expressions.

git grep -l -e 'term1' --and -e 'term2'

2) using --all-match flag

When giving multiple pattern expressions combined with --or, this flag is specified to limit the match to files that have lines to match all of them.

git grep -l --all-match -e 'term1' -e 'term2'

https://git-scm.com/docs/git-grep#git-grep--e

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • These options are producing different results for me. Option number 1 appears to require term1 and term2 be on the same line within the file, whereas option number 2 just requires both terms in the same file. I think the latter is more along the lines of what OP was asking. – pcronin Aug 21 '23 at 20:45
0

You can combine it with another grep command:

git grep -l -E 'term1' | grep term2
rammelmueller
  • 1,092
  • 1
  • 14
  • 29