450

I can search exact matches from Google by using quotes like "system <<-".

How can I do the same thing for GitHub?

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Just a learner
  • 26,690
  • 50
  • 155
  • 234

12 Answers12

248

You couldn't (before 2022). The official GitHub searching rules:

Due to the complexity of searching code, there are a few restrictions on how searches are performed:

  • Only the default branch is considered. In most cases, this will be the master branch.
  • Only files smaller than 384 KB are searchable.
  • Only repositories with fewer than 500,000 files are searchable.
  • You must always include at least one search term when searching source code. For example, searching for language:go is not valid, while amazing language:go is.
  • At most, search results can show two fragments from the same file, but there may be more results within the file.
  • You can't use the following wildcard characters as part of your search query:
    . , : ; / \ ` ' " = * ! ? # $ & + ^ | ~ < > ( ) { } [ ]
    The search will simply ignore these symbols.

Update: GitHub supports literal strings now, but you can also try some more powerful ways below.


Try Sourcegraph

For complex search with regex support try Sourcegraph.

enter image description here


Clone and use git-grep:

git support searching in sources with git-grep command. Just clone a repository and use the command in the folder:

git grep "text-to-search"

Alternatives:

I recommend you to try ripgrep tool, it's fast and simple. Works like git-grep but looks nicer:

rg "text-to-search"

And you can use the standard grep to search any text in files:

grep -r "text-to-search" /repository
djvg
  • 11,722
  • 5
  • 72
  • 103
DenisKolodin
  • 13,501
  • 3
  • 62
  • 65
  • 548
    This is terrible. Why don't they fix it? Ideally I should be able to search with a regex. Where do I go to protest this? :) – Daniel Darabos Feb 02 '16 at 10:38
  • 18
    I've asked a support and they responsed: "The ability to escape special characters in GitHub search is a frequently-requested feature. While I can't make any promises, I will +1 the idea on our internal Feature Request list.". I think it depends on index size: too many code github contains. – DenisKolodin Feb 04 '16 at 12:48
  • 13
    I believe this is an indexing challenge. Making the verbatim double-quote search work is prob an order of magnitude harder than present implementation. Making a regex search work is likely >1 orders of magnitude harder or even possibly [asking for trouble](https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS). – Matt Kocaj Aug 23 '16 at 03:44
  • 1
    I'd like to add info missing in Github's docs: **Another major restriction**: *Phrases with dashes* are ignored entirely, e.g.: `Bxp-pI-XMk`, `"Bxp-pI-XMk"` & `Bxp-pI-XMk in:file filename:designable.nib` all return 0 results for [TermiT/Flycut](https://github.com/TermiT/Flycut). Strange I know, as `Bxp-pI-XMk` is plainly seen in [designable.nib](https://github.com/TermiT/Flycut/blob/master/English.lproj/MainMenu.nib/designable.nib). No wildcard chars in the phrase, the file's on master + <384k *and* the example uses a "search term" and not filter values in isolation. – james_womack Mar 31 '17 at 03:02
  • @Xelian you can use the `user:username` syntax for that - the rest needs to be in quotes (`""`) – makevoid Apr 18 '17 at 20:38
  • 49
    what about literal strings that contain dot...like `ldap.mycomp.com`?!?!?! I don't need a regex, I just want a LITERAL string search – Jason May 25 '18 at 19:54
  • 2
    This is not fix because they use elasticsearch, so they should re-index all the data with a new field to take it in consideration, this means lot of disk. – Thomas Decaux Feb 04 '19 at 20:43
  • 1
    So basically no boolean logic operators for search, right? – Ricardo Barros Lourenço Mar 31 '22 at 20:22
  • 1
    I used sourcegraph and it worked a treat. For anyone wondering how to search a *specific repo* on sourcegraph, see [here](https://stackoverflow.com/questions/72223797/search-github-repo-for-term-with-special-characters-using-sourcegraph). – stevec May 14 '22 at 02:25
  • 14
    How do you use literal search strings now that it's 2022 ? – Didier A. Oct 24 '22 at 23:41
  • @DidierA. A year late, but, perhaps [query for an exact match](https://docs.github.com/en/search-github/github-code-search/understanding-github-code-search-syntax#query-for-an-exact-match)? – djvg Sep 01 '23 at 11:35
69

You can use Google directly.

How about this?

"your_string_to_search" site::https://github.com
"your_string_to_search" site::https://gist.github.com
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • 174
    Doesn't that assume that Google has access to your repo? That shouldn't be the case for private repos. – Joel B Apr 14 '16 at 18:04
  • @JoelB I don't know, I think google index only open repos. – mrgloom Apr 15 '16 at 08:53
  • 2
    it works inside repos, like: `"alias agi=" site::https://github.com/robbyrussell/oh-my-zsh` – sites Aug 02 '16 at 14:36
  • 1
    @juanpastas can you give full example of search string? – mrgloom Aug 02 '16 at 15:35
  • 29
    This made more sense back when Google still honoured the quotes to force an exact search. – Kylotan Mar 29 '17 at 13:22
  • could we filter by language using this? – FullMetalFist Aug 21 '18 at 20:36
  • 2
    @FullMetalFist Looks like yes, for example `"your_string_to_search" site:https://github.com filetype:cpp` – mrgloom Sep 10 '18 at 12:14
  • 1
    Would be good but doesn't work for two reasons: it'll ignore some special characters like `:` and the result list is not as usable as a site-specific result list could be, for example, things are just dumped, no idea where the hits are in the source tree – Robert Monfera Jan 30 '19 at 20:25
  • +1 Google is awesome! Was looking for the C# code files that implement all the xUnit Assert functionality and was able to track it down via this search on Google: `inurl:github.com/xunit filetype:cs assert` – Eric Mutta May 22 '21 at 16:23
  • 1
    Doesn't correctly match non-alphanumeric characters. Those are instead treated something like the regex `[^_\W]+`. This is a problem when you're searching code expressions. – Ouroborus Sep 24 '22 at 23:03
33

Today I was trying to look for an exact match of filter class in files named logback.xml in any repo on Github. And I came up with the following query which did the job.

"filter class" in:file filename:logback.xml

To enable exact matches with quotes you need to follow your search with the "in:file" modifier. The matches are not quite exact, the word "class" will have to follow the word "filter", but it seems there can be 0 or more spaces or symbols characters between the two words.

Preview
  • 35,317
  • 10
  • 92
  • 112
cessationoftime
  • 896
  • 8
  • 17
21
  1. Open a repository on GitHub, for example microsoft/fluentui
  2. Press dot "." to open VS Code web interface
  3. Go to search in the left panel
  4. Enable indexing via the prompt below search bar
  5. Huraaay! exact search works

UPDATE: As of November 2022, the solution above only works if you are signed in on GitHub.

You can enable preview of new search experience on this link: https://github.com/features/code-search-code-view/signup.

Then do exact match just by using quotes: "system <<-"

Draex_
  • 3,011
  • 4
  • 32
  • 50
  • 1
    vscode? could you explain how does it relate to github? – Sisir Oct 21 '21 at 17:39
  • 4
    @Sisir When you open a repository on GitHub.com and press dot `.`, it'll start running VS Code in your browser, with the repository open. – Draex_ Oct 22 '21 at 12:22
  • 1
    Thanks, saved my day since cs.github.com makes you sign up on whitelist – Encyklopedie Jan 08 '22 at 23:55
  • 1
    That's a local _repo_ search not a _GitHub search_. One is a SaaS for `git` and the other is a local `git` repo. -1 – Ari May 28 '22 at 11:32
  • 1
    @Ari It is a `repo` search, but not a local repo search. The key is that you don't need to download the repo before searching. – Draex_ May 29 '22 at 12:03
  • @Draex_ you might not be downloading repo but "indexing" is still caching on your local machine. Regardless this question was how to search _all of GitHub_ and not one repo! – Ari May 30 '22 at 22:56
  • I want to search 1000s of repositories at once – wilmol Oct 25 '22 at 21:12
  • This assumes you have some kind of vscode installed? – cgseller Nov 21 '22 at 13:35
  • @cgseller No, but when I test it in an incognito window, it no longer works. I guess you have to be signed in on GitHub. – Draex_ Nov 21 '22 at 14:12
13

You can: Since Dec. 2021, your search, done from cs.github.com, can include special characters

Improving GitHub code search

(from Pavel Avgustinov)

Search for an exact string, with support for substring matches and special characters, or use regular expressions (enclosed in / separators).

So "system <<-" should work, on that new search site.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 6
    It tells me to sign up for the technology preview and I signed up successfully but how many days will take to accept me to start using the feature? The default search in Github is sick and unuseful. Finally can't believe that they did it. – Taha Sami Dec 10 '21 at 13:22
  • 4
    @Taha I agree, and there seems to be some kind of waiting list there... – VonC Dec 10 '21 at 13:39
  • 3
    Almost two years later, still in technology preview. :-( – Emile Cormier Aug 07 '22 at 16:38
  • 1
    @EmileCormier True. Latest news: https://github.blog/2021-12-15-a-brief-history-of-code-search-at-github/ – VonC Aug 07 '22 at 21:35
11

Adding to @mrgloom's answer, if you're looking for code in a specific programming language in Github using Google you could do something like this in Google's search bar:

  • state the specific string you're looking for using the "intext:" search operator
  • add the programming language you're interested in, using the "ext:" operator (i.e. "ext:py", "ext:R", "ext:rb", etc.)
  • search in all public repos in Github using the "site:" operator mrgloom mentioned.

Example:

intext:"%% 2 == 0" ext:R site:github.com

Google Results from the example

silviaegt
  • 317
  • 3
  • 12
  • 2
    `intext:".set(" ext:JS site:github.com` didn't work :/ – Aditya M P Jul 18 '19 at 10:38
  • 1
    You're right! I don't understand why... I tried `intext:"set(" inurl:.js site:github.com` and it kinda works, but not really – silviaegt Jul 22 '19 at 21:59
  • 3
    So I kept reading and apparently [parenthesis are ignored in google searches](https://support.google.com/websearch/forum/AAAAgtjJeM4I-nOsFWviRs/?hl=en&gpf=%23!msg%2Fwebsearch%2FI-nOsFWviRs%2FOSXB_PyB0yoJ&msgid=OSXB_PyB0yoJ) However, I don't understand why the "ext" operator doesn't work. According to [this](http://www.googleguide.com/advanced_operators_reference.html#ext) it is an undocumented alias for "filetype", but not sure why "js" would not be considered as one – silviaegt Jul 22 '19 at 22:12
4

As of 11/2/2021, this is possible by putting quotation marks around your search string

Without quotes: Searching chaos monkey on GitHub with unquoted terms

With quotes: Searching chaos monkey on GitHub with string

While it's now possible to search exact strings, the functionality doesn't yet support searching on non-alphanumeric characters. Example:

Searching chaos monkey on GitHub with question mark in quoted string

  • 3
    Good explanation. I am amazed (in a bad way) how poor github search is! – cb4 Dec 01 '21 at 16:49
  • 2
    I was unable to duplicate this behavior. I searched for a string with dots in it like a version number and got hundreds of thousands of results in my repos. – cgseller Nov 21 '22 at 13:38
  • @cgseller A dot is an example of what this answer calls a special character. I've updated the answer to say 'non-alphanumeric character' to avoid this misunderstanding. – Jacob Archambault Jul 12 '23 at 23:14
3

If your package is in debian, you can use their code search, which supports regular expressions: https://codesearch.debian.net/

Jan Katins
  • 2,219
  • 1
  • 25
  • 35
2

You can now do regex searches in GitHub using forward slashes rather than quotes, so you can match both exact strings and patterns. Try the search /system <<-/ for an exact match, or /system[\s]*<<-/ for any number of whitespace characters in the middle, for example!

GitHub reference: Understanding GitHub Code Search syntax > Using regular expressions

cokeman19
  • 2,405
  • 1
  • 25
  • 40
Matthew Read
  • 1,365
  • 1
  • 30
  • 50
1

If your search term is a filename or other substring which contains punctuation characters, a partial workaround to get GitHub's code search to return instances of that substring is to (1) replace the punctuation characters in your search term with spaces, and (2) enclose the search term in quotes.

For example, instead of using the search term:

  • repo:my_repo my_image_asset_1.svg

Try:

  • repo:my_repo "my image asset 1 svg"

This might not be a perfect solution in all cases; I imagine it might also match filenames like my-image-asset-1.svg. But depending on your use case, it might be "good enough"?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
0

If you quickly want to search inside a specific repo, try that:

  • Press . while viewing the repo to open it inside a browser-based VS Code window
  • Enter your search term into the menu on the left
  • Enable indexing
    enter image description here
garzj
  • 2,427
  • 2
  • 8
  • 15
0

The searching query MUST be wrapped between / (slashes)

Example 1

Search for occurrence of query = """, will look like this /query = """/

Example 2

Search for query = """ in test_*.py files, will look like this path:**/test_*.py /query = """/

Notes

Use GitHub Search or Advanced GitHub Search - be aware that the advanced search might still generate some unrecognized qualifiers (e.g. filename:)

Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124