# Git Grep: search tracked files

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/git-grep-full-en
`git grep -n PATTERN` searches the tracked files and prints file name and line number. It does not look into untracked files.

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

(Test: 3 tests were run, TE-73-A, TE-73-B and TE-73-C. The expected non-zero exit is TE-73-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes are in the evidence table.)

### TE-73-A: git grep -n finds a tracked file and skips an untracked one

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'needle\nhay\n' > tracked.txt; git add tracked.txt; git commit -qm one
printf 'needle\n' > untracked.txt
```

Command:

```bash
git grep -n needle
```

Recorded output:

```
tracked.txt:1:needle
```

Exit status: 0. The output matches the expected text written before the run.

### TE-73-B: git grep --untracked also searches a file that git does not track

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'needle\nhay\n' > tracked.txt; git add tracked.txt; git commit -qm one
printf 'needle\n' > untracked.txt
```

Command:

```bash
git grep -n --untracked needle | sort
```

Recorded output:

```
tracked.txt:1:needle
untracked.txt:1:needle
```

Exit status: 0. This status and the output match what was written before the run. The status is that of sort, the last command in the pipe.

### TE-73-C: expected non-zero exit: git grep with no match exits with status 1 and prints nothing

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'needle\nhay\n' > tracked.txt; git add tracked.txt; git commit -qm one
```

Command:

```bash
git grep nosuchword
```

Recorded output: none, standard output was empty.

Exit status: 1. This status and the output match what was written before the run.

TE-73-B: by the setup, tracked.txt is committed and untracked.txt is not. With --untracked, git grep printed a match from both files, and sort put them in file order. TE-73-C: git grep nosuchword exited with status 1. Standard output and standard error were both empty.

## Limits

The run uses one fixed string. Regular expression options were not tested. Not tested on this page: `--cached` (search the index instead of the work tree), `--no-index` (find contents that git does not manage), `--exclude-standard` (ignore the files named by `.gitignore`), `--recurse-submodules`, `-v` (show non-matching lines), `-i` (ignore case), `-w` (match only at word boundaries), `-a` and `-I` (treat binary files as text, or skip them), `--max-depth`, `-c` (show the number of matches), `-l` and `-L` (show only file names, with or without a match), `-o` (show only the matching parts of a line), `-A`, `-B` and `-C` (context lines), `-p` and `-W` (show the function name or the surrounding function), `-f` (read patterns from a file), `--and`, `--or`, `--not`, `-q` and `--all-match`. The names and descriptions come from `git grep -h` in git 2.34.1. No `git grep` command here used them.

## Source

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

## Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) |
|---|---|---|---|---|
| `TE-73-A` | git grep -n finds a tracked file and skips an untracked one | 0 | PASS | `7da5af571968` |
| `TE-73-B` | git grep --untracked also searches a file that git does not track | 0 | PASS | `fa070814bd18` |
| `TE-73-C` | expected non-zero exit: git grep with no match exits with status 1 and prints nothing | 1 | PASS | `91819203ec78` |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC.
Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

## Related guides
- [Git Rm Cached: stop tracking a file and keep it on disk](https://kairos-2z6s.onrender.com/res/git-rm-cached-full-en)
- [Git Shortlog: commits per author](https://kairos-2z6s.onrender.com/res/git-shortlog-full-en)
- [git log: commits reachable from HEAD, and --all](https://kairos-2z6s.onrender.com/res/git-log-filters-full-en)
- [git diff: unstaged, staged, and against a commit](https://kairos-2z6s.onrender.com/res/git-diff-forms-full-en)
- [.gitignore patterns: which files Git treats as untracked](https://kairos-2z6s.onrender.com/res/gitignore-patterns-full-en)
