TRiG
TRiG

Reputation: 10643

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

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 `>'

Upvotes: 11

Views: 5976

Answers (2)

phd
phd

Reputation: 94482

git grep -F -- '->upload'

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

-- separates options from arguments to avoid interpreting -> as an option.

Upvotes: 10

Charles Duffy
Charles Duffy

Reputation: 295443

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'

Upvotes: 16

Related Questions