How to grep in Git
Finding specific code patterns or text across a large repository is essential for debugging, refactoring, and understanding code dependencies. As the creator of CoreUI, I’ve searched through countless large codebases for specific implementations. Git grep is a powerful built-in command that searches for patterns in tracked files, respecting .gitignore and offering better performance than regular grep. This approach searches only version-controlled files and can search across different branches and commits.
Use git grep to search for text patterns efficiently across your Git repository.
# Basic search for a pattern
git grep 'function'
# Case-insensitive search
git grep -i 'TODO'
# Search for whole words only
git grep -w 'user'
# Show line numbers
git grep -n 'export'
# Count matches per file
git grep -c 'import'
# Search in specific file types
git grep 'className' -- '*.jsx' '*.js'
# Search for pattern in specific directory
git grep 'useState' -- src/components/
# Search for multiple patterns (OR)
git grep -e 'useState' -e 'useEffect'
# Search and show function context
git grep -p 'handleClick'
# Search in specific branch
git grep 'apiKey' main
# Search in specific commit
git grep 'PASSWORD' abc1234
# Search with regex
git grep -E 'function\s+\w+\('
Best Practice Note
Git grep is significantly faster than regular grep because it only searches tracked files and uses Git’s internal indexing. The -p flag shows the function or class name containing the match, which is extremely useful for context. Use --and to combine multiple patterns that must all match, or --not to exclude patterns. For searching commit messages instead of file content, use git log --grep. This is how we search CoreUI repositories—using git grep to quickly locate component implementations, find usage patterns, and identify technical debt across thousands of files.



