Skip to content

Instantly share code, notes, and snippets.

@chernjie
Created January 28, 2026 22:12
Show Gist options
  • Select an option

  • Save chernjie/0a66e47946fdd2df87b272b5e8d5a8b4 to your computer and use it in GitHub Desktop.

Select an option

Save chernjie/0a66e47946fdd2df87b272b5e8d5a8b4 to your computer and use it in GitHub Desktop.
git find - combined git-grep and git-ls-files
#!/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