420

Is there a way to grep for something inside a Github project's code?

I could pull the source and grep it locally, but I was wondering if it's possible through the web interface or a 3rd-party alternative.

Ideas?

benui
  • 6,440
  • 5
  • 34
  • 49

12 Answers12

302

Update May 2023:

The new code search and code view is now generally available (May. 2023)

At GitHub Universe last year, we announced a total redesign of GitHub's code search and navigation experience, powered by our all-new code search engine that we built from scratch.
And in February, we announced our public beta.

Today, we are rolling out this feature to all GitHub users.

https://i0.wp.com/user-images.githubusercontent.com/78169714/236855021-b5f60b03-78e4-46ac-a89d-7712cfb85aca.png?ssl=1 -- Screenshot of code search results

Check out our blog post to learn more about how GitHub's new code search and code view can help you search, navigate, and understand your code.
And if you have feedback, please share it with us in our feedback discussion.


Update Dec. 2021: search has been improved again, with Search for an exact string, with support for substring matches and special characters, or regexps.

regex

But only on cs.github.com, and still in beta (waitlist applies)


Update January 2013: a brand new search has arrived!, based on elasticsearch.org:

A search for stat within the ruby repo will be expressed as stat repo:ruby/ruby, and will now just workTM.
(the repo name is not case sensitive: test repo:wordpress/wordpress returns the same as test repo:Wordpress/Wordpress)

enter image description here

Will give:

enter image description here

And you have many other examples of search, based on followers, or on forks, or...


Update July 2012 (old days of Lucene search and poor code indexing, combined with broken GUI, kept here for archive):

The search (based on SolrQuerySyntax) is now more permissive and the dreaded "Invalid search query. Try quoting it." is gone when using the default search selector "Everything":)

(I suppose we can all than Tim Pease, which had in one of his objectives "hacking on improved search experiences for all GitHub properties", and I did mention this Stack Overflow question at the time ;) )

Here is an illustration of a grep within the ruby code: it will looks for repos and users, but also for what I wanted to search in the first place: the code!

GitHub more permissive search results


Initial answer and illustration of the former issue (Sept. 2012 => March 2012)

You can use the advanced search GitHub form:

  • Choose Code, Repositories or Users from the drop-down and
  • use the corresponding prefixes listed for that search type.

For instance, Use the repo:username/repo-name directive to limit the search to a code repository.
The initial "Advanced Search" page includes the section:

Code Search:

The Code search will look through all of the code publicly hosted on GitHub. You can also filter by :

  • the language language:
  • the repository name (including the username) repo:
  • the file path path:

So if you select the "Code" search selector, then your query grepping for a text within a repo will work:

Good Search selector


What is incredibly unhelpful from GitHub is that:

  • if you forget to put the right search selector (here "Code"), you will get an error message:
    "Invalid search query. Try quoting it."

Wrong selector for the code filer

  • the error message doesn't help you at all.
    No amount of "quoting it" will get you out of this error.

  • once you get that error message, you don't get the sections reminding you of the right association between the search selectors ("Repositories", "Users" or "Language") and the (right) search filters (here "repo:").
    Any further attempt you do won't display those associations (selectors-filters) back. Only the error message you see above...
    The only way to get back those arrays is by clicking the "Advance Search" icon:

Advance Search Icon on GitHub

  • the "Everything" search selector, which is the default, is actually the wrong one for all of the search filters! Except "language:"...
    (You could imagine/assume that "Everything" would help you to pick whatever search selector actually works with the search filter "repo:", but nope. That would be too easy)

  • you cannot specify the search selector you want through the "Advance Search" field alone!
    (but you can for "language:", even though "Search Language" is another combo box just below the "Search for" 'type' one...)

Wrong search selector


So, the user's experience usually is as follows:

  • you click "Advanced Search", glance over those sections of filters, and notice one you want to use: "repo:"
  • you make a first advanced search "repo:jruby/jruby stat", but with the default Search selector "Everything"
    => FAIL! (and the arrays displaying the association "Selectors-Filters" is gone)
  • you notice that "Search for" selector thingy, select the first choice "Repositories" ("Dah! I want to search within repositories...")
    => FAIL!
  • dejected, you select the next choice of selectors (here, "Users"), without even looking at said selector, just to give it one more try...
    => FAIL!
  • "Screw this, GitHub search is broken! I'm outta here!"
    ...
    (GitHub advanced search is actually not broken. Only their GUI is...)

So, to recap, if you want to "grep for something inside a Github project's code", as the OP Ben Humphreys, don't forget to select the "Code" search selector...

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • are you sure, i tried `repo:"jruby/jruby" stat` `repo:"jruby" stat` `jruby: stat` none of which works – Tom Mar 13 '12 at 12:35
  • @TomH: at first glance, it might be a bug on GitHub side, as commented at http://superuser.com/a/179900/141 – VonC Mar 13 '12 at 12:59
  • @TomH: after an helpful edit from "Community", I have added illustrations to my answer in order to show how the advanced search `repo:jruby/jruby stat` (no need for double-quotes here) can work. So it is not a bug, after all. – VonC Mar 18 '12 at 10:41
  • @Community: your edit was helpful, thank you! But it lacked a bit of some illustrations and analysis. I have edited the answer to include those. – VonC Mar 18 '12 at 10:44
  • How do I search the current, master branch only? I don't want to see multiple hits on the same file (as shown in the first screen shot) when all but one are obsolete. – Mark Berry Mar 19 '12 at 18:55
  • @MarkBerry good question. I don't see any specific filter at first glance. – VonC Mar 19 '12 at 19:42
  • I found I could fork the project, make the fork private, then search using the "Search source code" box that only appears on private repositories (see answer by @Patrick Linskey). That's a lot of work! – Mark Berry Mar 20 '12 at 00:23
  • Okay - this is great. What steps can we take to feed this information back to github so they fix it? – Danny Staple May 17 '12 at 10:41
  • 1
    @DannyStaple Well, they *seem* to be aware of the issue, if you look at the mission described in https://github.com/blog/1116-tim-pease-is-a-githubber: "Tim will most likely be [...] hacking on improved search experiences for all GitHub properties". Make sure to reply to their GitHub tweet (https://twitter.com/#!/github/status/197070106768048128), like I did (https://twitter.com/#!/VonC_/status/197565733830541313) – VonC May 17 '12 at 11:25
  • 9
    FWIW I contacted GitHub because I have not been getting any search results when restricting searches to my repos. Tim Pease responded that they were having difficulty with the volume of repo indexes, so they've been migrating to sharded index servers. There's no ETA on when all repos will be migrated. – sourcenouveau Jul 19 '12 at 13:05
  • @emddudley is there an github issue somewhere we can track for that? – studgeek Aug 30 '12 at 01:29
  • 3
    I have just used the advice of this answer and it is working for me as well, but I have one caveat to add. The repo name should always be in lowercase even if GitHib lists it everywhere else in a different way. For example, (after setting the Code dropdown, etc, as above) try the same search with: repo:wordpress/wordpress vs repo:WordPress/WordPress ... only the first one will yield results. – ethanpil Jan 11 '13 at 00:36
  • @ethanpil and remember that for some *new* repos... you won't get any result at all: http://stackoverflow.com/a/14182550/6309 – VonC Jan 11 '13 at 06:29
  • @ethanpil I have update my answer, and your case-sensitive repo name issue is now gone. – VonC Jan 24 '13 at 06:52
  • @lajarre it would be interesting to post a *new* question (referencing back this one), with more details about what exactly isn't "actually working". – VonC Apr 23 '13 at 05:18
  • They changed the syntax apparently, it’s now `@user:repo` rather than `repo:user/repo` – Konrad Rudolph May 01 '13 at 14:09
  • Can you search partial words? `repo:git/git path:/Documentation/RelNotes/1.7.2.txt 'orphan'` is an exact match but `repo:git/git path:/Documentation/RelNotes/1.7.2.txt 'orpha'` fails :( – sabgenton Nov 28 '13 at 10:39
  • @sabgenton I agree, it doesn't seem to work. That would be a good question to ask to Github support, and then to Stack Overflow (where you can answer your own question), for all to benefit from. – VonC Nov 28 '13 at 10:41
  • Ok done, they said they'd make it a feature request but in the mean time just clone and grep yourself. Be nice if it came through. – sabgenton Nov 28 '13 at 16:32
  • @sabgenton incidentally, that is what I do: clone and grep, using http://stackoverflow.com/a/14871646/6309 and http://stackoverflow.com/a/2928721/6309. – VonC Nov 28 '13 at 16:33
  • Does anyone else see this answer as a ginormous **cry for help?** There's got to be a better way than this pig-pile of features. – Bob Stein Jul 02 '19 at 20:40
  • how to search in 2021? – MWO Dec 18 '21 at 09:11
  • 1
    @MWO In 2021, you would need to register to cs.github.com: see https://github.blog/2021-12-08-improving-github-code-search/ and https://cs.github.com/about. – VonC Dec 18 '21 at 13:58
  • @VonC wow that seems promising. registered now – MWO Dec 18 '21 at 14:17
79

To seach within a repository, add the URL parametes /search?q=search_terms at the root of the repo, for example:

https://github.com/bmewburn/vscode-intelephense/search?q=phpstorm

enter image description here

In the above example, it returns 2 results in Code and 160 results in Issues.

kiatng
  • 2,847
  • 32
  • 39
  • This is cool! Do you know how to get it working for branches of the main repo? When I followed the format you provided it didn't work for me. – piedpiper Jan 15 '21 at 02:00
  • 1
    As of now: _Only the default branch is indexed for code search._ See https://docs.github.com/en/free-pro-team@latest/github/searching-for-information-on-github/searching-code – kiatng Jan 15 '21 at 05:53
23

Recent private repositories have a search field for searching through that repo.

enter image description here

Bafflingly, it looks like this functionality is not available to public repositories, though.

Kamlesh
  • 5,233
  • 39
  • 50
Patrick Linskey
  • 1,124
  • 1
  • 13
  • 24
  • 1
    I wonder if this is a lowly commercial move to make people upgrade to the paying version? Doesn't sound like github but still. – Sylvain Feb 06 '12 at 13:09
  • 3
    @Sylvain It don't think it is done as an encouragement to upgrade, as even after upgrading, you won't get that search field for public repositories. Instead, I think that main search uses an index which doesn't include private repositories, and so you don't loose functionality when paying for a private repository, they had to add another way to search through that private repository only, hence the additional text field. – avernet Feb 29 '12 at 23:43
  • 2
    Imagine the overhead of them having a search index for every line of code in every repo! Insane! That's why they only do it for private repos.. – Jonny Nott Nov 14 '12 at 12:10
  • 2
    The search is now in the header, same as global search. – Tib May 20 '13 at 13:00
20

UPDATE

The bookmarklet hack below is broken due to XHR issues and API changes.

Thankfully Github now has "A Whole New Code Search" which does the job superbly.


Checkout this voodoo: Github code search userscript.

Follow the directions there, or if you hate bloating your browser with scripts and extensions, use my bookmarkified bundle of the userscript:

javascript:(function(){var s='https://raw.githubusercontent.com/skratchdot/github-enhancement-suite/master/build/github-enhancement-suite.user.js',t='text/javascript',d=document,n=navigator,e;(e=d.createElement('script')).src=s;e.type=t;d.getElementsByTagName('head')[0].appendChild(e)})();doIt('');void('');

Save the source above as the URL of a new bookmark. Browse to any Github repo, click the bookmark, and bam: in-page, ajaxified code search.

CAVEAT Github must index a repo before you can search it.

Before the Bookmarklet

Abracadabra...

After - Look in the second menubar after the leftmost tabs: Files, Commits, Branches...

Here's a sample search from the annotated ECMAScript 5.1 specification repository:

Sample search in the annotated ECMAScript 5.1 specification repository

alper
  • 2,919
  • 9
  • 53
  • 102
fny
  • 31,255
  • 16
  • 96
  • 127
  • When I use the plugin (Chrome) it works fine in the scratchdot/github-code-search repo, but not in any other repo. – forforf Apr 03 '12 at 15:17
  • It should work in any repo's home page. Perhaps you're trying from a different page? I posted a screenshot of a sample search in a different repository in my post. – fny Apr 03 '12 at 22:09
  • Aha! I actually discovered why those searches may have failed: a repo must wait its turn for indexing. Given the zillion active repositories, I suspect that takes a bit of time. – fny Apr 30 '12 at 03:35
  • Love the bookmarklet idea, but not compliant with modern browsers. – user456584 Nov 22 '12 at 00:20
  • 2
    @user456584 that's news to me. You might want to let [Google](http://support.google.com/chrome/bin/answer.py?hl=en&answer=100215) and [Mozilla](http://support.mozilla.org/en-US/kb/bookmarklets-perform-common-web-page-tasks) know too. – fny Nov 23 '12 at 07:15
  • 2
    I figured out why the plugin isn't working, its because the repo name needs to be in lower case. (See my comment to VonC's answer above). I already submitted a patch on GitHub, hopefully the original author will see it. https://github.com/skratchdot/github-code-search.user.js/pull/5 – ethanpil Jan 11 '13 at 03:29
  • 1
    Seems like search option is not working anymore on Github code search userscript. – alper Aug 02 '20 at 13:34
11

While @VonC's answer works for some repositories, unfortunately for many repositories you can't right now. Github is simply not indexing them (as commented originally by @emddudley). They haven't stated this anywhere on their website, but they will tell you if you ask support:

From: Tim Pease
We have stopped adding newly pushed code into our codesearch index. The volume of code has outgrown our current search index, and we are working on moving to a more scalable search architecture. I'm sorry for the annoyance. We do not have an estimate for when this new search index will be up and running, but when it is ready a blog post will be published (https://github.com/blog).

Annoyingly there is no way to tell which repositories are not indexed other than the lack of results (which also could be from a bad query).

There also is no way to track this issue other than waiting for them to blog it (or watching here on SO).

From: Tim Pease
I am afraid our issue tracker is internal, but we can notify you as soon as the new search index is up and running.

studgeek
  • 14,272
  • 6
  • 84
  • 96
  • They should at least mention that some search results may be omitted. It totally sucks when I refactor code and I search for occurrences of code that are there but not mentioned by GitHub. – Nicodemuz May 17 '16 at 07:46
10

There is a really nice project called github1s @ https://github.com/conwnet/github1s.

Basically you change the url of any repo https://github.com/microsoft/vscode to https://github1s.com/microsoft/vscode and then you can use the built in search function in the editor, to search for anything in a repository.

JohnDoe
  • 507
  • 7
  • 21
  • Similarly to github1s, GitHub also supports online vscode editor on `github.dev`. So you can view the code at `https://github.dev/microsoft/vscode`. Read more [here](https://docs.github.com/en/codespaces/the-githubdev-web-based-editor). – Tal Jacob - Sir Jacques Jul 29 '22 at 11:54
7

Go here: https://github.com/search and enter "pattern repo:user_name/repo_name".

For example, to search for cnn_learner in the fastai repo of user fastai, enter this:

cnn_learner repo:fastai/fastai

That's it. The only annoyance is you'll need an extra click. It will tell you:

We couldn’t find any repositories matching 'cnn_learner repo:fastai/fastai'

because by default it searches for repositories matching that search string...

So just click on the left on "Code" and it will display what you want.

Or get the code search results directly with a URL like this:

https://github.com/search?q=cnn_learner+repo%3Afastai%2Ffastai&type=code

Kai Carver
  • 2,240
  • 29
  • 23
  • This seems to be broken in 2022 and results in this error: "The requested content has not yet been indexed, please try again in a few moments." How is it that github has no code search in 2022 other than an ongoing closed beta? – Jeff Axelrod Nov 13 '22 at 14:20
  • huh, the search by URL seems to work fine still, for example searching for "html" in the code of repository "hbd" of "kaicarver": https://github.com/search?q=html+repo%3Akaicarver%2Fhbd&type=code or search for "monaco" in the code of repository "vscode" of "microsoft" https://github.com/search?q=monaco+repo%3Amicrosoft%2Fvscode&type=code – Kai Carver Nov 15 '22 at 10:01
7

Use your search term(s) and in:file in the Search box.

The Search will create a dropdown that asks you where you want to search.

enter image description here

smoore4
  • 4,520
  • 3
  • 36
  • 55
4

Simply use SourceGraph.com instead to search in GitHub, or install its extension:

I search the source code inside of Github Repositories with the free Sourcegraph Chrome Extension

I skimmed through SourceForge's Chrome Extension Docs and looked at just what I needed for searching for directory names with Github's Search Engine itself by reading some of Github's Codebase Searching Doc

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
3

Visit that repo's homepage, then type your search term into the search box on the top left.

The first suggested result will be tagged "in this repository". Click on that, or hit enter.

enter image description here

enter image description here

RedDragonWebDesign
  • 1,797
  • 2
  • 18
  • 24
2

Google allows you to search in the project, but not the code :(

site:https://github.com/CreateJS/EaselJS test

crizCraig
  • 8,487
  • 6
  • 54
  • 53
2

GitHub documentation says -

octocat in:file matches code where "octocat" appears in the file contents.

octocat in:path matches code where "octocat" appears in the file > path.

octocat in:file,path matches code where "octocat" appears in the > file contents or the file path.

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87