You can search for code by using search qualifiers in any combination to narrow your search results.

Code in forks is only searchable if the fork has more stars than the parent repository. To include forks with more stars than their parent in the search results, you will need to add fork:true or fork:only to your query. For more information, see "Searching in forks."

Tips:

  • This article contains example searches on the GitHub.com website, but you can use the same search filters on your GitHub Enterprise instance.
  • For a list of search syntaxes that you can add to any search qualifier to further improve your results, see "Understanding the search syntax".
  • Use quotations around multi-word search terms. For example, if you want to search for issues with the label "In progress," you'd search for label:"in progress". Search is not case sensitive.

Note: You must be logged in to search for code across all public repositories.

Considerations for code search

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

  • Only the default branch is indexed for code search. In most cases, this will be the master branch.
  • Forks with fewer stars than the parent repository are not indexed for code search. For more information, see "Searching in forks."
  • Only files smaller than 5 MB are searchable.
  • Only the first 500 KB of each file is searchable.
  • Only repositories with fewer than 500,000 files are searchable.
  • Logged in users can search all public repositories while anonymous searches must include a limit on org:, user:, or repo:.
  • Except with filename searches, you must always include at least one search term when searching source code. For example, searching for language:javascript is not valid, while amazing language:javascript 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.

Scope the search fields

The in qualifier limits what fields are searched. With this qualifier, you can restrict your search to the source code, the file path, or both. Without the qualifier, only the file contents are searched.

Qualifier Example
in:file octocat in:file matches code where "octocat" appears in the file contents.
in:path octocat in:path matches code where "octocat" appears in the path name.
octocat in:file,path matches code where "octocat" appears in the file contents or the path name.

Search by language

You can search for code based on what language it's written in.

Qualifier Example
language:LANGUAGE element language:xml size:100 matches code with the word "element" that's marked as being XML and has exactly 100 bytes.
display language:scss matches code with the word "display," that's marked as being SCSS.
org:mozilla language:markdown matches code from all @mozilla's repositories that's marked as Markdown.

Search by the source code file size

The size qualifier uses greater than, less than, and range qualifiers to filter results based on the byte size of the file in which the code is found.

Qualifier Example
size:n function size:>10000 language:python matches code with the word "function," written in Python, in files that are larger than 10 KB.

Search by the location of a file within the repository

By including the path qualifier, you specify that resulting source code must appear at a specific location in a repository. Subfolders are considered during the search, so be as specific as possible. You can also use path:/ to restrict the search results to the root level of the project.

Qualifier Example
path:PATH/TO/DIRECTORY console path:app/public language:javascript matches JavaScript files with the word "console" in an app/public directory or its subdirectories (even if they reside in app/public/js/form-validators).
path:DIRECTORY form path:cgi-bin language:perl matches Perl files with the word "form" in a cgi-bin directory.
path:/ octocat filename:readme path:/ matches files named readme with the word "octocat" located at the root level of a repository.

Search by filename

You can use the filename qualifier if there's a specific file you're looking for.

Qualifier Example
filename:FILENAME filename:linguist matches files named "linguist."
filename:.vimrc commands matches .vimrc files with the word "commands."
filename:test_helper path:test language:ruby matches Ruby files named test_helper within the test directory.

Search by the file extension

The extension qualifier matches code files with a certain file extension.

Qualifier Example
extension:EXTENSION form path:cgi-bin extension:pm matches code with the word "form," under cgi-bin, with the .pm file extension.
icon size:>200000 extension:css matches files larger than 200 KB that end in .css and have the word "icon."

Search within a user's or organization's repositories

To grab a list of code from all repositories owned by a certain user or organization, you can use the user or org qualifier. For getting a list of code from a specific repository, you can use the repo qualifier.

Qualifier Example
user:USERNAME user:defunkt extension:rb matches code from @defunkt that ends in .rb.
org:ORGNAME org:github extension:js matches code from GitHub that ends in .js.
repo:USERNAME/REPOSITORY repo:mozilla/shumway extension:as matches code from @mozilla's shumway project that ends in .as.

Further reading