How to search files in a Git repository with git grep
Running git grep <word> searches the files that Git manages for lines containing that word. It is useful when you want to find where a function or variable is used, or when you want to search the whole project while leaving out node_modules and build output. Files ignored by .gitignore and files you have not yet run git add on are not searched. I ran the Git commands in a test environment and include the output as it appeared.
The test environment was Ubuntu 24.04, Git 2.43.0, and GNU grep 3.11 for comparison. The author name and email were set to example values (Neko <neko@example.com>). The output was written to a file and captured, so there are no color codes. I did not test on Windows or macOS, differences in regex types such as -P, or searching inside submodules.
- 1. The repository before searching
- 2. Comparing grep -r with git grep
- 3. Showing line numbers (-n)
- 4. Controlling case and word boundaries (-i, -w)
- 5. Listing only file names or counts (-l, -c)
- 6. Limiting the search location (paths after –)
- 7. Combining multiple words (-e, –and, –not)
- 8. Showing the enclosing function (-p)
- 9. Grouping output by file (–heading, –break)
- 10. Searching untracked files too (–untracked)
- 11. Searching past commits and tags
- 12. Checking whether a match exists with the exit status
The repository before searching
I prepared a repository called neko-app. It contains src/cart.js, which calculates shipping, docs/guide.md, a description, and tests/cart.test.js, a test. There are two commits. The first commit is tagged v1.0.
$ git ls-files
.gitignore
docs/guide.md
src/cart.js
tests/cart.test.js
$ git log --oneline
1c6b4b2 3000円以上で送料無料にする
2993652 最初のコミット
The contents of src/cart.js are as follows.
const SHIPPING_FEE = 500;
const FREE_SHIPPING_LINE = 3000;
function calcShipping(sum) {
// Shipping is free for orders of 3000 yen or more
if (sum >= FREE_SHIPPING_LINE) return 0;
return SHIPPING_FEE;
}
function calcTotal(items) {
const sum = items.reduce((a, i) => a + i.price, 0);
return sum + calcShipping(sum);
}
module.exports = { calcTotal, calcShipping };
I also placed a memo that is not under Git management (memo.txt), and node_modules/ and dist/, which are ignored by .gitignore. The case differs, but every one of them contains the word "shipping".
$ git status --short --ignored
?? memo.txt
!! dist/
!! node_modules/
Comparing grep -r with git grep
First, I searched for "shipping" with plain grep -r, case-insensitive (-i). I excluded only the .git folder.
$ grep -rn -i shipping . --exclude-dir=.git
./src/cart.js:1:const SHIPPING_FEE = 500;
./src/cart.js:2:const FREE_SHIPPING_LINE = 3000;
./src/cart.js:4:function calcShipping(sum) {
./src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
./src/cart.js:7: return SHIPPING_FEE;
./src/cart.js:12: return sum + calcShipping(sum);
./src/cart.js:15:module.exports = { calcTotal, calcShipping };
./dist/bundle.js:1:const SHIPPING_FEE=500;
./memo.txt:1:TODO: shipping の表示を確認する
./node_modules/shop/index.js:1:var shipping = 0;
./docs/guide.md:3:送料(shipping)は500円です。
./docs/guide.md:4:3000円以上で送料無料(free shipping)になります。
The results include dist/, memo.txt, and node_modules/. Running the same search with git grep returned only src/cart.js and docs/guide.md, which are the files Git manages.
$ git grep -i shipping
docs/guide.md:送料(shipping)は500円です。
docs/guide.md:3000円以上で送料無料(free shipping)になります。
src/cart.js:const SHIPPING_FEE = 500;
src/cart.js:const FREE_SHIPPING_LINE = 3000;
src/cart.js:function calcShipping(sum) {
src/cart.js: if (sum >= FREE_SHIPPING_LINE) return 0;
src/cart.js: return SHIPPING_FEE;
src/cart.js: return sum + calcShipping(sum);
src/cart.js:module.exports = { calcTotal, calcShipping };
The official git grep reference says it searches the tracked files in the working tree. You do not need to specify folders to exclude each time; you only search the files that Git manages.
Showing line numbers (-n)
The git grep output above has no line numbers. Adding -n puts the line number after the file name.
$ git grep -n calcShipping
src/cart.js:4:function calcShipping(sum) {
src/cart.js:12: return sum + calcShipping(sum);
src/cart.js:15:module.exports = { calcTotal, calcShipping };
You can see that calcShipping is defined on line 4 and used on lines 12 and 15. All the examples that follow use -n.
Controlling case and word boundaries (-i, -w)
Without -i, the search is case-sensitive. Searching for "shipping" alone only matched the two lines in docs/guide.md that are written in lowercase.
$ git grep -n shipping
docs/guide.md:3:送料(shipping)は500円です。
docs/guide.md:4:3000円以上で送料無料(free shipping)になります。
With -w, only lines where the word matches as a whole word are shown. When I searched for SHIPPING with -w, nothing matched. The SHIPPING inside SHIPPING_FEE is followed by _, so it does not count as a whole word.
$ git grep -n -w SHIPPING
$ git grep -n -w SHIPPING_FEE
src/cart.js:1:const SHIPPING_FEE = 500;
src/cart.js:7: return SHIPPING_FEE;
The official reference for -w requires that the matched text be preceded and followed by either the start or end of the line, or a non-word character. These results show that _ is treated as part of a word.
Listing only file names or counts (-l, -c)
Use -l when you only want to know which files contain matches, and -c when you want the number of matching lines per file.
$ git grep -l -i shipping
docs/guide.md
src/cart.js
$ git grep -c -i shipping
docs/guide.md:2
src/cart.js:7
Limiting the search location (paths after –)
Writing folders or files after -- restricts the search to those paths. I searched for "shipping" in src only.
$ git grep -n -i shipping -- src
src/cart.js:1:const SHIPPING_FEE = 500;
src/cart.js:2:const FREE_SHIPPING_LINE = 3000;
src/cart.js:4:function calcShipping(sum) {
src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
src/cart.js:7: return SHIPPING_FEE;
src/cart.js:12: return sum + calcShipping(sum);
src/cart.js:15:module.exports = { calcTotal, calcShipping };
Wildcards such as "*.js" also work. When I searched for calcTotal, matches were found in .js files in subfolders too, such as src/ and tests/.
$ git grep -n calcTotal -- "*.js"
src/cart.js:10:function calcTotal(items) {
src/cart.js:15:module.exports = { calcTotal, calcShipping };
tests/cart.test.js:1:const { calcTotal } = require("../src/cart");
tests/cart.test.js:2:console.log(calcTotal([{ price: 100 }]) === 600);
Conversely, to exclude a specific folder, put :! before its name. I searched for calcTotal while excluding the tests.
$ git grep -n calcTotal -- ':!tests'
src/cart.js:10:function calcTotal(items) {
src/cart.js:15:module.exports = { calcTotal, calcShipping };
:! is a Git pathspec, and the gitglossary official reference describes it as an exclude pathspec. You can also use ^ in place of !.
Combining multiple words (-e, –and, –not)
Listing words with -e shows lines that contain any one of them. I searched for lines containing "FREE" or "500".
$ git grep -n -e FREE -e 500
docs/guide.md:3:送料(shipping)は500円です。
src/cart.js:1:const SHIPPING_FEE = 500;
src/cart.js:2:const FREE_SHIPPING_LINE = 3000;
src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
To show only lines that contain both, put --and between them. To show lines that do not contain a word, use --not.
$ git grep -n -e SHIPPING --and -e 500
src/cart.js:1:const SHIPPING_FEE = 500;
$ git grep -n -e shipping --and --not -e free
docs/guide.md:3:送料(shipping)は500円です。
The second command shows lines that contain "shipping" and do not contain "free". According to the official reference for –and, when you write nothing, the patterns are joined with --or, and when you combine them with --and or --not, every pattern needs its own -e.
Showing the enclosing function (-p)
With -p, the output also shows which function each matching line is in. I searched for return.
$ git grep -n -p return
src/cart.js=4=function calcShipping(sum) {
src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
src/cart.js:7: return SHIPPING_FEE;
src/cart.js=10=function calcTotal(items) {
src/cart.js:12: return sum + calcShipping(sum);
Lines separated by = are the function name lines, and lines separated by : are the matching lines. You can tell that lines 6 and 7 are inside calcShipping, and line 12 is inside calcTotal.
Function name lines are chosen by the same rules as the hunk headers in git diff output (official -p reference). According to the gitattributes official reference on defining a custom hunk header, if you configure nothing, lines starting with a letter, _, or $ are chosen. Indented lines are not chosen, so in this example the lines that define the functions were shown.
Grouping output by file (–heading, –break)
When there are many matching lines, --heading prints each file name once at the top, and --break inserts a blank line between files, which makes the output easier to read.
$ git grep -n --heading --break -i free
docs/guide.md
4:3000円以上で送料無料(free shipping)になります。
src/cart.js
2:const FREE_SHIPPING_LINE = 3000;
6: if (sum >= FREE_SHIPPING_LINE) return 0;
Searching untracked files too (–untracked)
git grep does not search files that Git does not manage, even when you name them. When I specified memo.txt, node_modules, and dist, the command printed nothing and exited with status 1.
$ git grep -n -i shipping -- memo.txt node_modules dist; echo "exit=$?"
exit=1
To include files you have not yet run git add on, add --untracked. When I put this option after the search word, it caused an error.
$ git grep -n -i shipping --untracked
fatal: option '--untracked' must come before non-option arguments
As the message says, moving --untracked before the word made memo.txt appear in the search.
$ git grep -n -i --untracked shipping
docs/guide.md:3:送料(shipping)は500円です。
docs/guide.md:4:3000円以上で送料無料(free shipping)になります。
memo.txt:1:TODO: shipping の表示を確認する
src/cart.js:1:const SHIPPING_FEE = 500;
src/cart.js:2:const FREE_SHIPPING_LINE = 3000;
src/cart.js:4:function calcShipping(sum) {
src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
src/cart.js:7: return SHIPPING_FEE;
src/cart.js:12: return sum + calcShipping(sum);
src/cart.js:15:module.exports = { calcTotal, calcShipping };
node_modules/ and dist/ still do not appear with --untracked alone. To include what .gitignore excludes as well, add --no-exclude-standard.
$ git grep -n -i --untracked --no-exclude-standard shipping
dist/bundle.js:1:const SHIPPING_FEE=500;
docs/guide.md:3:送料(shipping)は500円です。
docs/guide.md:4:3000円以上で送料無料(free shipping)になります。
memo.txt:1:TODO: shipping の表示を確認する
node_modules/shop/index.js:1:var shipping = 0;
src/cart.js:1:const SHIPPING_FEE = 500;
src/cart.js:2:const FREE_SHIPPING_LINE = 3000;
src/cart.js:4:function calcShipping(sum) {
src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
src/cart.js:7: return SHIPPING_FEE;
src/cart.js:12: return sum + calcShipping(sum);
src/cart.js:15:module.exports = { calcTotal, calcShipping };
With this, the same set of files that the first grep -r found is now covered. The official reference for –no-exclude-standard also says this option only has an effect when used together with --untracked.
Searching past commits and tags
When you put a tag, branch name, or commit after the search word, the search covers the contents at that point, not the current files. You do not need to switch branches.
$ git grep -n FREE v1.0; echo "exit=$?"
exit=1
$ git grep -n SHIPPING_FEE v1.0
v1.0:src/cart.js:1:const SHIPPING_FEE = 500;
v1.0:src/cart.js:5: return sum + SHIPPING_FEE;
At v1.0, there was no FREE for free shipping yet, and SHIPPING_FEE was used on line 5. Each result line starts with v1.0:, which shows the revision the result came from.
If you list two commits, both are searched. When I searched for FREE in HEAD~1 (the previous commit) and HEAD, it matched only in HEAD.
$ git grep -n FREE HEAD~1 HEAD
HEAD:src/cart.js:2:const FREE_SHIPPING_LINE = 3000;
HEAD:src/cart.js:6: if (sum >= FREE_SHIPPING_LINE) return 0;
Checking whether a match exists with the exit status
When nothing matches, the command prints nothing and exits with status 1. With -q, it does not print matching lines even when there are matches. It only returns exit status 0.
$ git grep -n neko; echo "exit=$?"
exit=1
$ git grep -q calcTotal; echo "exit=$?"
exit=0
The official reference for -q also says it exits with 0 when a match is found and with a non-zero status when no match is found. You can use it with if in a shell script to check that a word no longer remains.
Finally, I checked the state of the working tree after searching. As before the search, git grep did not change any files.
$ git status --short --ignored
?? memo.txt
!! dist/
!! node_modules/




