# How to Use Git Grep with Regex Patterns and Boolean Operators for Powerful Code Search

> Master Git grep for powerful code search. Learn to use regex patterns with boolean operators like AND OR NOT for complex searches across all Git revisions.

- Repository: [Tim Green/github-cheat-sheet](https://github.com/tiimgreen/github-cheat-sheet)
- Tags: how-to-guide
- Published: 2026-03-06

---

**Use `git grep` with the `-e` flag to specify POSIX extended regular expressions, then chain multiple patterns using `--and`, `--or`, and `--not` to execute complex Boolean searches across any Git revision.**

The `git grep` command is Git's native search utility that outperforms standard grep by respecting the index and searching across branches, tags, and commits. According to the `tiimgreen/github-cheat-sheet` repository—specifically the documentation in [`README.md`](https://github.com/tiimgreen/github-cheat-sheet/blob/main/README.md) between lines 884 and 906—you can leverage regex patterns and Boolean logic to pinpoint exactly where code patterns appear in your project history.

## Searching with Regular Expressions

Unlike basic string matching, `git grep` supports POSIX Extended Regular Expression syntax when you use the `-e` flag. This allows you to search for patterns rather than literals.

To search for lines beginning with optional whitespace followed by `TODO`:

```bash
git grep -e '^\s*TODO'

```

The `-e` flag explicitly marks the argument as a regex pattern, ensuring special characters like `^` and `\s` are interpreted correctly rather than treated as literal strings.

## Combining Patterns with Boolean Operators

`git grep` enables complex queries by joining multiple `-e` patterns with Boolean operators. These operators are evaluated in the order they appear, allowing you to construct precise search logic.

**`--and`** requires both patterns to match the same line.  
**`--or`** accepts lines matching either pattern.  
**`--not`** excludes lines matching the subsequent pattern.

To find lines containing `FIXME` and a digit, but excluding any line with the word `test`:

```bash
git grep -e 'FIXME' --and -e '[0-9]' --not -e 'test'

```

You can group multiple clauses to create sophisticated queries. The `tiimgreen/github-cheat-sheet` source emphasizes that operator order matters—patterns are combined sequentially as they appear in the command.

## Limiting Search Scope by Revision or Index

`git grep` can search beyond the working directory into Git's object database.

- **`--cached`** searches only the index (staged changes).
- **`--no-index`** searches untracked files, behaving like standard system `grep`.
- **`--branches`**, **`--tags`**, **`--remotes`** search across all references of that type.

To search for the string `config` only in staged changes:

```bash
git grep -e 'config' --cached

```

To search across every branch for a pattern:

```bash
git grep --branches -e 'api_key'

```

## Controlling Output Format

Customize results using output control flags:

- **`-n`** displays line numbers (default behavior in many configurations).
- **`-l`** lists only filenames containing matches, suppressing line content.
- **`-c`** shows a count of matching lines per file.

To get a summary count of files containing deprecated function calls:

```bash
git grep -c -e 'oldFunctionName'

```

## Practical Code Examples

The following examples demonstrate real-world usage patterns documented in the `tiimgreen/github-cheat-sheet` repository.

**Find commented TODO items:**

```bash
git grep -e '^\s*#.*TODO'

```

**Locate deprecated functions or magic numbers while excluding minified JavaScript:**

```bash
git grep -e 'oldFunc' --or -e '[0-9]\{4\}' --not -e '\.min\.js$'

```

**Search a specific release tag for function definitions:**

```bash
git grep -e 'function\s\+create' v2.1.0

```

**Find error messages in log files that do not mention timeout:**

```bash
git grep -e 'error' --and -e '\.log$' --not -e 'timeout'

```

## Summary

- Use `git grep -e '<pattern>'` to enable POSIX extended regular expressions for pattern matching.
- Chain multiple patterns with `--and`, `--or`, and `--not` to perform Boolean logic on search results.
- Search across Git history using `--branches`, `--tags`, or specific commit references like `HEAD~3`.
- Limit scope to staged changes with `--cached` or include untracked files with `--no-index`.
- Control output using `-n` for line numbers, `-l` for filenames only, or `-c` for match counts.

## Frequently Asked Questions

### What is the difference between `git grep` and regular `grep`?

`git grep` is optimized for Git repositories and can search tracked files across any revision, branch, or tag, whereas standard `grep` only examines the current working directory files. Additionally, `git grep` respects `.gitignore` and the Git index by default, automatically excluding untracked files unless you explicitly use `--no-index`.

### How do I search for multiple patterns that must all appear in the same file but not necessarily the same line?

The Boolean operators `--and`, `--or`, and `--not` in `git grep` apply to single lines, not entire files. To find files containing multiple patterns regardless of line position, use `git grep -l` with each pattern separately and pipe the results, or use `git grep` with the `--all-match` flag which requires all specified patterns to match somewhere in the file.

### Can I use Perl-compatible regex (PCRE) with `git grep`?

Yes, Git supports Perl-compatible regular expressions when you add the `--perl-regexp` (or `-P`) flag. This enables advanced features like lookahead and lookbehind assertions that are not available in the default POSIX Extended Regular Expression syntax used by `git grep -e`.

### Why does my `git grep` regex work on Linux but fail on macOS?

macOS uses the BSD regex library by default, which has stricter POSIX compliance than GNU regex libraries used on most Linux distributions. For consistent behavior across platforms, use character classes like `[[:space:]]` instead of `\s`, or install GNU grep via Homebrew and use `ggrep` instead of the system default.