yes

GitHub Code Search Cheat Sheet

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.

GitHub Search Box

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.

GitHub Search Filter Side Menu

Keywords

  • org:foo-bar - search within a organization
  • repo:my-service - search within a repository
  • user:octocat - search within a personal account
  • language:csharp - narrow down to a specific language
  • path:/test - search within file paths
  • content:README.md - restrict a search to strictly match the content
  • symbol:MyClass- search for symbol definitions*

GitHub Search Terms

*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 files
  • path:*.ts* Files with extension starting .ts
  • path:src/*.js Direct descendants
  • path:/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.

GitHub Save Search

Searches can be managed by typing in saved: opening a new dialog. GitHub Manage Save Search

Hitting letter t navigates to a file.

GitHub Navigate File

Hitting period . opens a lightweight code editor.

GitHub Light Editor


Read more from GitHub: https://docs.github.com/en/search-github/github-code-search/understanding-github-code-search-syntax