JacobIRR
JacobIRR

Reputation: 8966

Finding files with both of two specified terms via git grep

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...

Upvotes: 2

Views: 1002

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

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

Upvotes: 2

rammelmueller
rammelmueller

Reputation: 1118

You can combine it with another grep command:

git grep -l -E 'term1' | grep term2

Upvotes: 0

Related Questions