About code search query structure
The search syntax in this article only applies to searching code with GitHub code search. Note that the syntax and qualifiers for searching for non-code content, such as issues, users, and discussions, is not the same as the syntax for code search. For more information on non-code search, see About searching on GitHub and Searching on GitHub.
Search queries consist of search terms, comprising text you want to search for, and qualifiers, which narrow down the search.
A bare term with no qualifiers will match either the content of a file or the file's path.
For example, the following query:
http-push
The above query will match the file docs/http-push.txt, even if it doesn't contain the term http-push. It will also match a file called example.txt if it contains the term http-push.
You can enter multiple terms separated by whitespace to search for documents that satisfy both terms.
For example, the following query:
sparse index
The search results would include all documents containing both the terms sparse and index, in any order. As examples, it would match a file containing SparseIndexVector, a file with the phrase index for sparse trees, and even a file named index.txt that contains the term sparse.
Searching for multiple terms separated by whitespace is the equivalent to the search hello AND world. Other boolean operations, such as hello OR world, are also supported. For more information about boolean operations, see Using boolean operations.
Code search also supports searching for an exact string, including whitespace. For more information, see Query for an exact match.
You can narrow your code search with specialized qualifiers, such as repo:, language: and path:. For more information on the qualifiers you can use in code search, see Using qualifiers.
You can also use regular expressions in your searches by surrounding the expression in slashes. For more information on using regular expressions, see Using regular expressions.
Query for an exact match
To search for an exact string, including whitespace, you can surround the string in quotes. For example:
"sparse index"
You can also use quoted strings in qualifiers, for example:
path:git language:"protocol buffers"
Searching for quotes and backslashes
To search for code containing a quotation mark, you can escape the quotation mark using a backslash. For example, to find the exact string name = "tensorflow", you can search:
"name = \"tensorflow\""
To search for code containing a backslash, \, use a double backslash, \\.
The two escape sequences \\ and \" can be used outside of quotes as well. No other escape sequences are recognized, though. A backslash that isn't followed by either " or \ is included in the search, unchanged.
Additional escape sequences, such as \n to match a newline character, are supported in regular expressions. See Using regular expressions.
Using boolean operations
Code search supports boolean expressions. You can use the operators AND, OR, and NOT to combine search terms.
By default, adjacent terms separated by whitespace are equivalent to using the AND operator. For example, the search query sparse index is the same as sparse AND index, meaning that the search results will include all documents containing both the terms sparse and index, in any order.
To search for documents containing either one term or the other, you can use the OR operator. For example, the following query will match documents containing either sparse or index:
sparse OR index
To exclude files from your search results, you can use the NOT operator. For example, to exclude files in the __testing__ directory, you can search:
"fatal error" NOT path:__testing__
You can use parentheses to express more complicated boolean expressions. For example:
(language:ruby OR language:python) AND NOT path:"/tests/"
Using qualifiers
You can use specialized keywords to qualify your search.
- Repository qualifier
- Organization and user qualifiers
- Enterprise qualifier
- Language qualifier
- License qualifier
- Path qualifier
- Symbol qualifier
- Content qualifier
- Is qualifier
Repository qualifier
To search within a repository, use the repo: qualifier. You must provide the full repository name, including the owner. For example:
repo:github-linguist/linguist
To search within a set of repositories, you can combine multiple repo: qualifiers with the boolean operator OR. For example:
repo:github-linguist/linguist OR repo:tree-sitter/tree-sitter
Note
Code search does not currently support regular expressions or partial matching for repository names, so you will have to type the entire repository name (including the user prefix) for the repo: qualifier to work.
Organization and user qualifiers
To search for files within an organization, use the org: qualifier. For example:
org:github
To search for files within a personal account, use the user: qualifier. For example:
user:octocat
Note
Code search does not currently support regular expressions or partial matching for organization or user names, so you will have to type the entire organization or user name for the qualifier to work.
Enterprise qualifier
To search for files within an enterprise, use the enterprise: qualifier. For example:
enterprise:octocorp
This searches repositories owned by organizations in the octocorp enterprise. User-owned repositories are not included.
Language qualifier
To narrow down to a specific language, use the language: qualifier. For example:
language:ruby OR language:cpp OR language:csharp
For a complete list of supported language names, see languages.yaml in github-linguist/linguist. If your preferred language is not on the list, you can open a pull request to add it.
License qualifier
To filter repositories based on their license or license family, use the license: qualifier and the exact license keyword, for example Apache-2.0, CC, MIT.
license:MIT
See Licensing a repository for a list of license keywords.
Path qualifier
To search within file paths, use the path: qualifier. This will match files containing the term anywhere in their file path. For example, to find files containing the term unit_tests in their path, use:
path:unit_tests
The above query will match both src/unit_tests/my_test.py and src/docs/unit_tests.md since they both contain unit_test somewhere in their path.
To match only a specific filename (and not part of the path), you could use a regular expression:
path:/(^|\/)README\.md$/
Note that the . in the filename is escaped, since . has special meaning for regular expressions. For more information about using regular expressions, see Using regular expressions.