Powerful, flexible queries

Sourcegraph code search performs full-text searches and supports both regular expression and exact queries. By default, Sourcegraph searches across all your repositories. Our search query syntax allows for advanced queries, such as searching over any branch or commit, narrowing searches by programming language or file pattern, and more.

See the query syntax and query reference documentation for a comprehensive overview of supported syntax.

Sourcegraph supports advanced code search for specifically matching patterns inside code structures, like function parameters and loop bodies.

See the structural search documentation for a detailed explanation of this search mode.

Search over commit diffs using type:diff to see how your codebase has changed over time. This is often used to find changes to particular functions, classes, or areas of the codebase when debugging.

You can also search within commit diffs on multiple branches by specifying the branches in a repo: field after the @ sign. After the @, separate Git refs with :, specify Git ref globs by prefixing them with *, and exclude commits reachable from a ref by prefixing it with ^. Diff searches can be further narrowed down with parameters that filter by author and time.

See the query syntax documentation for a comprehensive list of supported parameters.

Searching over commit messages is supported in Sourcegraph by adding type:commit to your search query. Separately, you can also use the message:"any string" parameter to filter type:diff searches for a given commit message. Commit message searches can narrowed down further with filters such as author and time.

See our query syntax documentation for a comprehensive list of supported parameters.

Searching for symbols makes it easier to find specific functions, variables, and more. Use the type:symbol filter to search for symbol results. Symbol results also appear in typeahead suggestions, so you can jump directly to symbols by name.

Saved searches

Saved searches let you save and describe search queries so you can easily monitor the results on an ongoing basis. You can create a saved search for anything, including diffs and commits across all branches of your repositories. Saved searches can be an early warning system for common problems in your code and a way to monitor best practices, the progress of refactors, etc.

See the saved searches documentation for instructions for setting up and configuring saved searches.

Search scopes

Every project and team has a different set of repositories they commonly work with and search over. Custom search scopes enable users and organizations to quickly filter their searches to predefined subsets of files and repositories. Instead of typing out the subset of repositories or files you want to search, you can save and select scopes using the search scope buttons whenever you need.


As you type a query, the menu below will contain suggestions based on the query. Use the keyboard or mouse to select a suggestion. For example, if your query is repo:foo file:\.js$ hello, the suggestions will consist of the list of files that match your query.

You can also type in the partial name of a repository or filename to quickly jump to it. For example, typing in just foo would show you a list of repositories (first) and files with names containing foo.


On a search results page, press the Stats button to view a language breakdown of all results matching the query. Each matching file is analyzed to detect its language, and line count statistics are computed as follows:

  • Query matches entire repositories (e.g., using only repo:): all lines (in all files) in matching repositories are counted.
  • Query matches entire files (e.g., using only file: or lang:): all lines in all matching files are counted.
  • Query matches text in files (e.g., using a term such as foo): all lines that match the query are counted.


  • If your search query was file:test and you had a single 100-line Java test file (and no other files whose name contains test), the statistics would show 100 Java lines.
  • If your search query was foo and that term appeared on 3 lines in Java files and on 1 line in a Python file, the statistics would show 3 Java lines and 1 Python line.

Tip: On the statistics page, you can enter an empty query to see statistics across all repositories.

Version contexts sunsetting

The version contexts feature is being sunset and will be replaced by search contexts. Search contexts support all features and capabilities of version contexts.

Existing version contexts are managed by site admins in site configuration under the experimentalFeatures.versionContexts setting. For example:

"experimentalFeatures": {
  "versionContexts": [
      "name": "srcgraph 3.15",
      "revisions": [
          "repo": "github.com/sourcegraph/sourcegraph",
          "rev": "3.15"
          "repo": "github.com/sourcegraph/src-cli",
          "rev": "3.11.2"

To specify the default branch, you can set "rev" to "HEAD" or "".

After setting some version contexts, users can select version contexts in the dropdown to the left of the search bar.

Multi-branch indexing experimental

The most common branch to search is your default branch. To speed up this common operation Sourcegraph maintains an index of the source code on your default branch. Some organizations have other branches that are regularly searched. To speed up search for those branches Sourcegraph can be configured to index up to 64 branches per repository.

Your site admin can configure indexed branches in site configuration under the experimentalFeatures.search.index.branches setting. For example:

"experimentalFeatures": {
  "search.index.branches": {
   "github.com/sourcegraph/sourcegraph": ["3.15", "develop"],
   "github.com/sourcegraph/src-cli": ["next"]

Indexing multiple branches will add additional resource requirements to Sourcegraph (particularly memory). The indexer will deduplicate documents between branches. So the size of your index will grow in relation to the number of unique documents. Refer to our resource estimator to estimate whether additional resources are required.

Search contexts

Search contexts help you search the code you care about on Sourcegraph. A search context represents a set of repositories at specific revisions on a Sourcegraph instance that will be targeted by search queries by default.

Every search on Sourcegraph uses a search context. Search contexts can be defined with the contexts selector shown in the search input, or entered directly in a search query.

Sourcegraph Cloud currently supports two pre-set search contexts:

  • Your personal context, context:@username, which automatically includes all repositories you add to Sourcegraph Cloud.
  • The global context, context:global, which includes all repositories on Sourcegraph Cloud.

If no search context is specified, context:global is used.

Private Sourcegraph instances support custom search contexts:

  • Contexts owned by a user, such as context:@username/context-name, which can be private to the user or public to all users on the Sourcegraph instance.
  • Contexts at the global level, such as context:example-context, which can be private to site admins or public to all users on the Sourcegraph instance.
  • The global context, context:global, which includes all repositories on the Sourcegraph instance.

This feature is currently under active development for self-hosted Sourcegraph instances and is disabled by default.

Your site admin can enable search contexts on your private instance in global settings using the following:

"experimentalFeatures": {  
  "showSearchContext": true,
  "showSearchContextManagement": true

Note: While version contexts are located in the site configuration, search contexts are enabled through the global settings.

Reload the page after saving changes to see search contexts enabled.

If you currently use version contexts, you can automatically convert your existing version contexts to search contexts. We recommend migrating to search contexts for a more intuitive, powerful search experience and the latest improvements and updates.

See the search contexts documentation to learn how to use and create search contexts.