Issue
git grep
seems to have simpler rules than regular GNU grep
, which will allow you to search for tabs and to href="https://stackoverflow.com/q/11234858/209139">escape special characters with a backslash. I am trying to find occurrences of the string ->upload
, and no way of escaping it seems to work.
How do I run git grep "->upload"
?
$ git grep ->upload
No output; return 0
$ git grep "->upload"
error: unknown switch `>'
git grep "\-\>upload"
No output; return error
$ git grep '->upload'
error: unknown switch `>'
Solution
When doubt, use a single-character class rather than a backslash to make a single character literal inside a regex:
git grep -e '-[>]upload'
Whereas the meaning of a backslash can be different depending on the specific character and the specific regex syntax in use, [>]
means the same thing consistently.
That said, the most immediate issue here isn't caused by the >
but by the leading dash, which makes the string indistinguishable from a list of options.
The -e
is needed not because of the >
, but because of the -
. Without it, ->upload
would be treated as a series of flags (->
, -u
, -p
, -l
, -o
, -a
, -d
).
That said, you can get away without the -e
by also moving the dash into a character class, thus making it no longer the first character on the command line:
git grep '[-][>]upload'
Answered By - Charles Duffy Answer Checked By - Dawn Plyler (WPSolving Volunteer)