As a developer, you've probably used to browsing code samples on Stack Overflow and various dev blogs. There's also GitHub, a formidable source of wisdom. You can easily search through all the repositories or narrow it down to your organization by using a couple of modifiers.
In the second half of 2022, GitHub updated their code searching engine and interface. You can use several modifiers and keywords to narrow the search and find what you're after. It even supports regular expressions!
This blog post is meant to be succinct reference for crafting search queries.
Let's get right to it.
Hitting slash /
activates the search.
Bare terms (keywords with no modifiers) search both paths and file content, within default branches.
Wrap the term inside quotes ""
for an exact match, e.g. "internal error"
You can use the side menu to exclude certain bits.
Keywords
org:foo-bar
- search within a organizationrepo:my-service
- search within a repositoryuser:octocat
- search within a personal accountlanguage:csharp
- narrow down to a specific languagepath:/test
- search within file pathscontent:README.md
- restrict a search to strictly match the contentsymbol:MyClass
- search for symbol definitions*
*Symbol extraction is supported for the following languages.
- C#
- Python
- Go
- Java
- JavaScript
- TypeScript
- PHP
- Protocol Buffers
- Ruby
- Rust
Boolean operators
- org:mac OR org:cheese
- (language:ruby OR language:python) NOT path:"/tests/"
Path Globbing
path:*.txt
All txt filespath:*.ts*
Files with extension starting .tspath:src/*.js
Direct descendantspath:/src/**/*.js
Include subdirectories
Regular expressions
Surround the regex in slashes //
.
Searching files within App/src directory: /^App\/src\//
Search for exact definition: symbol:/^MyMethod$/
Exclude certain file types from the results: org:foo-bar myKeyword NOT path:/\.(md|sql|yml)/
Bonus
You can also save the search. For example, if you frequently need to search across multiple repositories, you can search for (org:foo OR org:bar)
and press Save in the top right corner.
Searches can be managed by typing in saved:
opening a new dialog.
Hitting letter t
navigates to a file.
Hitting period .
opens a lightweight code editor.
Read more from GitHub: https://docs.github.com/en/search-github/github-code-search/understanding-github-code-search-syntax