# info > GIT-GREP

---
type: CommandReference
command: git-grep
mode: man
section: 1
source: man-pages
---

## Quick Reference
- `git grep 'pattern'` — search tracked files for pattern
- `git grep -i 'pattern'` — ignore case
- `git grep -n 'pattern'` — show line numbers
- `git grep -l 'pattern'` — list matching file names
- `git grep 'time_t' -- '*.[ch]'` — search in specific file types
- `git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)` — combine patterns with Boolean AND
- `git grep --all-match -e NODE -e Unexpected` — files containing lines that match all patterns
- `git grep solution -- :^Documentation` — exclude a directory from search

## Name
Print lines matching a pattern

## Synopsis
shell
git grep [options] [-e] <pattern>... [--] [<pathspec>...]
## Options

### Pattern matching syntax
- `-E, --extended-regexp` — use POSIX extended regexp
- `-G, --basic-regexp` — use POSIX basic regexp (default)
- `-P, --perl-regexp` — use Perl-compatible regexp
- `-F, --fixed-strings` — treat pattern as literal string
- `-i, --ignore-case` — case insensitive matching
- `-w, --word-regexp` — match only at word boundaries
- `-v, --invert-match` — select non-matching lines

### Combining patterns
- `-e <pattern>` — specify pattern; required for patterns starting with `-` and for multiple patterns
- `--and, --or, --not, ( ... )` — boolean operators; `--or` is default, `--and` has higher precedence
- `--all-match` — when using `--or`, only show files that have lines matching **all** patterns

### File selection
- `--cached` — search in staged changes (index) instead of working tree
- `--no-index` — search files in current directory not managed by Git
- `--untracked` — also search untracked files
- `--recurse-submodules` — recursively search in active submodules
- `--max-depth <depth>` — limit directory depth; `-1` for unlimited (default)
- `--no-exclude-standard` — include ignored files (useful with `--untracked`)
- `--exclude-standard` — exclude ignored files (with `--no-index`)

### Output control
- `-n, --line-number` — prefix line numbers
- `--column` — prefix 1‑indexed byte offset of the first match
- `-l, --files-with-matches` — only list file names with matches
- `-L, --files-without-match` — only list file names without matches
- `-c, --count` — show number of matching lines per file
- `-o, --only-matching` — show only the matching parts of lines
- `-h` — suppress filename prefix
- `-H` — show filename (default, overrides `-h`)
- `--full-name` — force paths relative to repository root
- `--break` — print blank line between matches from different files
- `--heading` — show filename as a heading above its matches
- `-p, --show-function` — show the preceding function name line
- `-A <num>, --after-context <num>` — show `<num>` trailing lines
- `-B <num>, --before-context <num>` — show `<num>` leading lines
- `-C <num>, --context <num>` — show `<num>` leading and trailing lines
- `-W, --function-context` — show the entire function containing the match
- `--color[=<when>]` — colorize matches; `always` (default), `never`, or `auto`
- `-q, --quiet` — suppress output; exit 0 if match found, non‑zero otherwise
- `-z, --null` — use NUL as pathname delimiter

### Other options
- `-f <file>` — read patterns from `<file>`, one per line
- `-O[<pager>], --open-files-in-pager[=<pager>]` — open matching files in pager
- `--threads <num>` — number of grep worker threads
- `-a, --text` — process binary files as text
- `-I` — skip binary files
- `--textconv` — honor textconv filter settings

## Examples

### Basic search
shell
git grep 'pattern'
Search for pattern in all tracked files.

### Case‑insensitive search
shell
git grep -i 'pattern'
### Show line numbers
shell
git grep -n 'pattern'
### List files with matches
shell
git grep -l 'pattern'
### Search in specific file types
shell
git grep 'time_t' -- '*.[ch]'
Look for `time_t` in all tracked `.c` and `.h` files.

### Combine patterns with AND
shell
git grep -e '#define' --and \( -e MAX_PATH -e PATH_MAX \)
Match lines that contain `#define` and either `MAX_PATH` or `PATH_MAX`.

### Files matching all patterns
shell
git grep --all-match -e NODE -e Unexpected
Files that have lines matching both `NODE` and `Unexpected` (anywhere in the file).

### Exclude a path
shell
git grep solution -- :^Documentation
Search for `solution`, excluding the `Documentation` directory.

## See Also
- [git(1)](http://localhost/phpMan.php/man/git/1/markdown)
- [git-config(1)](http://localhost/phpMan.php/man/git-config/1/markdown)
- [gitattributes(5)](http://localhost/phpMan.php/man/gitattributes/5/markdown)
- [gitglossary(7)](http://localhost/phpMan.php/man/gitglossary/7/markdown)