Is it possible to get git grep
to search only new or modified files in the index/cache?
(The use-case for this is to use in a pre-commit hook that looks for "debug" code such as console.log
in the prospective commit. But I'm not bothered by console.log
in "existing" code. Preferably this would also fail to match instances of console.log
that are removed, but I can live with those matching!)
`git grep` command is used to search in the checkout branch and local files. But if the user is searching the content in one branch, but the content is stored in another branch of the repository, then he/she will not get the searching output.
The git grep version will only search in files tracked by git, whereas the grep version will search everything in the directory. So far so similar; either one could be better depending on what you want to achieve.
Indexing. For every tracked file, Git records information such as its size, creation time and last modification time in a file known as the index. To determine whether a file has changed, Git compares its current stats with those cached in the index. If they match, then Git can skip reading the file again.
That's because git-grep doesn't need to bother to go through the filesystem for the on-disk files. It greps directly from the object storage instead. And AFAIK it can run the grep in parallel.
It turns out the way to do this is not via git grep
, but by a completely different command, that also happens to be able to search: git diff-index
. (Another triumph for simplicity and orthogonality...)
What I wanted can be achieved via:
$ git diff-index -U --cached -G <regexp> HEAD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With