You cannot find a function in the editor, but a colleague says it exists in the current commit. You may be searching different versions: working files, staged content or committed data. Git grep lets you select the source explicitly before drawing conclusions.
1. Start with a narrow scope
These examples assume a src directory and use calculateTotal as a sample keyword. Run from the repository root and adapt the term and path. The commands search without modifying files.
git grep -n -F -e 'calculateTotal' -- src/
-n includes line numbers, -F uses literal strings rather than regular expressions, -e identifies the pattern, and -- separates paths. The Git grep documentation describes the default source as tracked files in the working tree.
2. Choose working tree, index or commit
git grep -n -F -e 'calculateTotal' -- src/
git grep --cached -n -F -e 'calculateTotal' -- src/
git grep -n -F -e 'calculateTotal' HEAD -- src/
These search working files, index content and the HEAD snapshot respectively. Results can differ after a rename, partial staging and further editing. --cached searches file content in the index, not only newly staged lines.
Replace HEAD with a verified commit ID to inspect an older snapshot. This searches that snapshot, not the repository's entire change history.
3. When a new file is missing
Use git status --short to check whether the file is tracked. To include untracked files within the selected scope:
git grep --untracked -n -F -e 'calculateTotal' -- src/
Do not immediately expand to all ignored files: they may contain dependencies, builds or sensitive configuration. Establish where the target file belongs first. No result in one file set does not establish absence everywhere.
4. Match output to the question
git grep -l -F -e 'calculateTotal' HEAD -- src/
-l lists matching filenames, useful for narrowing the investigation before reading code. Return to -n for individual matches. Read surrounding context to distinguish actual calls from comments and test data.
String matching is not semantic analysis: names may be generated dynamically, aliased or used indirectly. Do not delete a function solely because a grep count suggests it is unused.
5. Investigation checklist
- Confirm the repository and path.
- Select working tree, index or commit explicitly.
- Distinguish literal matching from regex.
- Check untracked files before concluding there are no matches.
- Do not share output containing tokens or internal data.
- Review and test subsequent changes separately.
Git grep is useful not just for finding text, but for stating which version was searched. Recording that scope makes an investigation easier to reproduce.




No comments yet. Be the first to share your thoughts.