Created
January 28, 2026 22:12
-
-
Save chernjie/0a66e47946fdd2df87b272b5e8d5a8b4 to your computer and use it in GitHub Desktop.
git find - combined git-grep and git-ls-files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Search for a string in both filenames (git ls-files) and file contents (git grep) | |
| # Usage: git find <search-term> [path...] | |
| usage() { | |
| echo "Usage: git find <search-term> [path...]" >&2 | |
| exit 1 | |
| } | |
| main() { | |
| local grepspec=() pathspec=() | |
| for i in "$@" | |
| do test "$i" = "--" -o -d "$i" && pathspec+=("$i") || grepspec+=("$i") | |
| done | |
| test ${#pathspec[@]} -eq 0 && pathspec=("--" ".") | |
| git ls-files --recurse-submodules "${pathspec[@]}" | grep --color=auto "${grepspec[@]}" | |
| git grep --recurse-submodules --name-only "$@" | |
| } | |
| case $1 in | |
| '') usage ;; | |
| *) main "$@" ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment