gitsearchgithub

Search code inside a Github project


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?


Solution

  • 2023-2025+: https://github.com/search?type=Code

    Example inside a project: https://github.com/search?q=repo%3Agit%2Fgit+rebase&type=code

    Update May 2023:

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

    (As noted by holydragon in the comments, that does require to sign in to GitHub first)

    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:

    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:

    Wrong selector for the code filer

    Advance Search Icon on GitHub

    Wrong search selector


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


    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...