pythongitunixgrepgit-grep

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


Solution

  • 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