Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git grep: search through source code

git grep searches for text in files tracked by Git. Faster than regular grep, it automatically ignores node_modules and untracked files.

Search

What is git grep?

git grep searches for text in files tracked by Git. It is like grep but optimized for Git repositories.

The advantage: it automatically ignores untracked files (node_modules, build, .env) and can even search in old commits.

Git grep syntax

git grep "pattern"

Search in all tracked files

git grep -n "pattern"

With line numbers

git grep "pattern" -- "*.js"

Search only in JS files

Git grep in practice

Basic and advanced searches.

Basic search

Advanced search

Essential grep commands

git grep -i "word"

Case-insensitive search

git grep -n "word"

With line numbers

git grep -c "word"

Count occurrences per file

git grep -l "word"

List only file names

git grep -w "var"

Whole word (no substring)

git grep -E "regex"

Extended regular expression

Common mistakes with git grep

Searching in untracked files

git grep only searches tracked files. For untracked files, use git grep --untracked or regular grep.

Forgetting quotes around the pattern

Without quotes, the shell may interpret special characters. Always wrap the pattern in quotes: git grep "pattern".

Not using file filters

On large projects, filter by file type with -- "*.ext" for more relevant and faster results.

Part of the Git History, Search & Debug guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git grep

Master code search with GitQuest

Learn to dig through code and history like a detective.

Start the investigations