Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I git grep for a string including a ">"?

Tags:

git

bash

git grep seems to have simpler rules than regular GNU grep, which will allow you to search for tabs and to 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 `>'
like image 546
TRiG Avatar asked Dec 19 '17 16:12

TRiG


2 Answers

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'
like image 62
Charles Duffy Avatar answered Oct 02 '22 14:10

Charles Duffy


git grep -F -- '->upload'

Option -F means: use fixed strings for patterns (don't interpret pattern as a regex).

-- separates options from arguments.

like image 40
phd Avatar answered Oct 02 '22 15:10

phd