--- title: Git Grep date: 2024-04-09T13:38:41+01:00 tags: git identifier: "20240409T133841" --- # `git grep` To search for commit _content_ (i.e., actual lines of source, as opposed to commit messages and the like), you need to do: ``` git grep $(git rev-list --all) ``` `git rev-list --all | xargs git grep ` will work if you run into an "Argument list too long" error. If you want to limit the search to some subtree (for instance, "lib/util"), you will need to pass that to the `rev-list`subcommand and `grep` as well: ``` git grep $(git rev-list --all -- lib/util) -- lib/util ``` This will grep through all your commit text for `regexp`. The reason for passing the path in both commands is because `rev-list`will return the revisions list where all the changes to `lib/util` happened, but also you need to pass to `grep` so that it will only search in `lib/util`. Just imagine the following scenario: `grep` might find the same ``on other files which are contained in the same revision returned by `rev-list` (even if there was no change to that file on that revision). Here are some other useful ways of searching your source: Search working tree for text matching regular expression regexp: ``` git grep ``` Search working tree for lines of text matching regular expression regexp1 or regexp2: ``` git grep -e [--or] -e ``` Search working tree for lines of text matching regular expression regexp1 and regexp2, reporting file paths only: ``` git grep -l -e --and -e ``` Search working tree for files that have lines of text matching regular expression regexp1 and lines of text matching regular expression regexp2: ``` git grep -l --all-match -e -e ``` Search working tree for changed lines of text matching pattern: ``` git diff --unified=0 | grep ``` Search all revisions for text matching regular expression regexp: ``` git grep $(git rev-list --all) ``` Search all revisions between rev1 and rev2 for text matching regular expression regexp: ``` git grep $(git rev-list ..) ``` ## Resources https://stackoverflow.com/a/2929502