65

I want to do a case sensitive search for a particular piece of code in a GitHub repository. Is it possible?

I checked https://help.github.com/articles/searching-code/ and didn't find such a syntax rule.

Mark Jin
  • 2,616
  • 3
  • 25
  • 37

3 Answers3

28

Use grep or git grep in your local clone. The GitHub search has a lot of limitations. I think the worst of these is that it only shows the first two matches from any file, so I just avoid it whenever possible.

db48x
  • 3,108
  • 24
  • 16
  • Seems like this is the only way. Thanks! – Mark Jin Nov 16 '15 at 19:10
  • 4
    Though, You can not use it to search whole group or whole github.com this way. – Oliver Gondža Aug 23 '16 at 08:37
  • Is this still true? I would like to search for the string "LIB = " but it seems both ignoring the uppercase and the character "=". Thanks – desmond13 Nov 23 '16 at 11:07
  • 3
    That's fine for a single repository, or a small handful, but pretty frustrating when you're trying to search across an entire organisation. – Bart Read Jan 10 '18 at 16:28
  • I wrote an answer with a bunch of details on that: https://stackoverflow.com/questions/60843047/locating-a-function-in-a-git-repository/60843055#60843055 – Gabriel Staples Mar 27 '20 at 06:01
  • And if such a repository mas many GB of data (and you don't want to clone all these GB)? – pmor Oct 11 '21 at 21:12
  • If download speed is the problem, then rent a VPS. You can ssh into your VPS, clone the repository there, and then search it with `grep`, `git grep`, and `rg`. This costs money, but probably a lot less money than a faster internet connection. Also, if it saves you time then you can use that saved time to earn more money. – db48x Oct 11 '21 at 22:42
23

Say you want results for my_var but not MY_VAR. Use this regex search:

/(?-i)my_var/

The -i flag here is essentially the opposite of the same grep option (grep is case-sensitive by default and -i makes it case-insensitive).

This is a new option included in the rollout of GitHub's blog on new Code Search feature.

GitHub Docs: GitHub Code Search syntax

Andrew Lam
  • 3,675
  • 4
  • 23
  • 34
Matthew Read
  • 1,365
  • 1
  • 30
  • 50
5

There is an indirect way. Hit . (which will redirect you to github.dev) to open the repo in VSCode for the Browser.

There you can do a case-sensitive search.

Trass3r
  • 5,858
  • 2
  • 30
  • 45