Skip to main content
A Sourcegraph query consists of two parts: a search pattern describing what to find, and filters that scope where to look. Both are optional—you can use filters alone to search commit history, file names, or repository names without a content pattern.
<search pattern>  <filter> <filter> ...
For example:
http.NewRequest lang:go repo:myorg/ -file:_test\.go

Search patterns

Keyword search (default)

In keyword mode (the default since Sourcegraph 5.4), terms are matched independently anywhere in the file. Use quotes for phrases and /.../ for inline regex:
SyntaxBehavior
foo barFiles containing both foo and bar anywhere
"foo bar"Exact phrase match
/foo.*bar/Regular expression match (RE2 syntax)
foo OR barFiles containing either foo or bar
Matching is case-insensitive by default. Toggle the Aa button in the search bar or add case:yes to your query to enable case sensitivity.
Enable the (.*) toggle or include patternType:regexp to treat the entire query as a regular expression. Spaces between unquoted terms become .*? (fuzzy match). Quoted terms are matched literally.
SyntaxBehavior
foo barMatches foo(.*?)bar
"foo bar"Matches the literal string foo bar
foo\nbarMultiline match; \n is interpreted as a newline
foo\ bar or /foo bar/Matches foo bar with a literal space
Sourcegraph uses RE2 syntax. Lookaheads, lookbehinds, and backreferences are not supported. For post-filtering with PCRE features, pipe results through src CLI and jq. Structural search uses Comby patterns to match syntactic code structures. Use :[name] as a hole that matches any sequence of characters (including across lines):
fmt.Errorf(":[msg]", :[args])
if err := :[call]; err != nil { :[body] }
Structural search ignores whitespace differences, making it reliable across codebases with different formatting conventions. Enable it with the [] toggle or patternType:structural.
Structural search requires an indexed commit. It is not available for unindexed branches.

Filters (all searches)

Filters narrow which repositories, files, or branches are searched. Multiple filters of the same type are intersected (AND logic) unless you use | within a single filter value for OR logic.

Repository

FilterDescriptionExample
repo:patternInclude repos whose path matches the RE2 patternrepo:myorg/frontend
-repo:patternExclude matching repos-repo:archived-
repo:pattern@revSearch a specific revisionrepo:myorg/[email protected]
rev:revisionSearch a revision (used with repo:)repo:myorg/api rev:main
To search multiple revisions, separate them with ::
repo:^github\.com/myorg/api$ rev:v1.5:v2.0 deprecatedEndpoint
Use glob patterns to search across many branches:
repo:^github\.com/myorg/myrepo$@*refs/heads/ type:diff after:"1 week ago" TODO

File

FilterDescriptionExample
file:patternInclude files whose full path matches the RE2 patternfile:\.go$
-file:patternExclude matching files-file:_test\.go$
file:has.content(pattern)Only files whose contents match the patternfile:has.content(TODO)
file:has.owner(email)Only files with a matching CODEOWNERS entryfile:has.owner([email protected])
file:has.contributor(pattern)Only files with a matching contributor name or emailfile:has.contributor(alice)

Language

lang:typescript encoding
-lang:javascript http
Aliases: language:, l:. Language detection is based on file extension by default. Enable the search-content-based-lang-detection feature flag for content-based detection.

Content

Use content:"pattern" to set the search pattern explicitly when the pattern text would conflict with filter syntax:
repo:myorg content:"repo:myorg"
Use -content:"pattern" to exclude files containing a pattern:
file:Dockerfile alpine -content:alpine:latest

Result type

FilterDescription
type:symbolSymbol search (functions, classes, variables)
type:diffSearch commit diffs
type:commitSearch commit messages

Select

select: transforms results to return only a specific aspect:
ValueReturns
select:repoDistinct repository paths
select:fileDistinct file paths
select:contentMatching content chunks
select:symbol.functionOnly function symbols
select:symbol.classOnly class symbols
select:commit.diff.addedOnly added lines in diffs
select:commit.diff.removedOnly removed lines in diffs
select:file.directoryDirectory paths of matched files
select:file.ownersOwners of matched files
Example—find all repositories that use fmt.Errorf:
fmt.Errorf select:repo

Repository predicates

FilterDescriptionExample
repo:has.path(pattern)Repos containing a file path matching the patternrepo:has.path(\.py)
repo:has.content(pattern)Repos containing file content matching the patternrepo:has.content(TODO)
repo:has.topic(topic)Repos with a given GitHub/GitLab topicrepo:has.topic(security)
repo:has.meta(key:value)Repos tagged with a metadata key-value pairrepo:has.meta(team:platform)
repo:has.commit.after(time)Repos with a commit more recent than the given timerepo:has.commit.after(1 month ago)
repo:has.description(pattern)Repos with a description matching the patternrepo:has.description(go package)

Behavior modifiers

FilterDescriptionExample
case:yesCase-sensitive matchingOPEN_FILE case:yes
fork:yes / fork:onlyInclude or limit to forked reposfork:yes repo:myorg
archived:yes / archived:onlyInclude or limit to archived reposarchived:only repo:myorg
count:N / count:allReturn N results or wait for allcount:1000 http.Get
timeout:durationSet a custom search timeout (max 1 minute)timeout:30s count:10000 func
visibility:public / visibility:privateFilter by repo visibilitytype:repo visibility:public
patterntype:keyword / patterntype:regexpOverride the pattern typetest. patternType:regexp

Filters (diff and commit searches only)

These filters apply only when type:diff or type:commit is set:
FilterDescriptionExample
author:patternCommits authored by a matching name or emailtype:diff author:alice
-author:patternExclude commits by matching authortype:commit -author:bot
after:"time"Commits after the given timeafter:"6 weeks ago"
before:"time"Commits before the given timebefore:"2024-01-01"
message:"string"Commits whose message contains the stringtype:commit message:"fix auth"
-message:"string"Exclude commits with matching message-message:"WIP"
Time values accept many formats: "yesterday", "5 days ago", "2 weeks ago", "november 1 2019", "2024-03-15", and Unix timestamps. Example—find all security-related changes on any branch in the last week:
type:diff repo:^github\.com/myorg/api$@*refs/heads/ after:"1 week ago" \b(auth|security|password|permissions)\b

Boolean operators

Combine expressions with AND, OR, and NOT (case-insensitive):
OperatorBehaviorExample
ANDBoth sides must matchconf.Get( AND log15.Error(
OREither side must matchconf.Get( OR log15.Error(
NOTNegates a term or filterpanic NOT ever
AND binds more tightly than OR, so a AND b OR c AND d means (a AND b) OR (c AND d). Use parentheses to override precedence:
lang:go (timeout OR deadline) AND NOT test
When no parentheses are present, filters bind to adjacent terms:
file:main.c char c or (int i and int j)
is interpreted as (file:main.c char c) or (int i and int j). To apply file:main.c to the entire expression, use:
file:main.c (char c or (int i and int j))
To search for the literal words OR, AND, or NOT in code, use content:"query with OR".

Repository revisions

Append a revision to the repo: filter with @, or use the separate rev: filter:
# Branch name
repo:^github\.com/myorg/api$@main

# Commit hash
repo:^github\.com/myorg/api$@1735d48

# Tag
repo:^github\.com/myorg/[email protected]

# Multiple revisions
repo:^github\.com/myorg/api$ rev:v1.0:v2.0:main

# All branches (glob)
repo:^github\.com/myorg/api$@*refs/heads/

# All branches except main
repo:^github\.com/myorg/api$@*refs/heads/:^refs/heads/main

# Search at a point in time (Sourcegraph 5.4+)
repo:^github\.com/myorg/api$ rev:at.time(1 year ago)

Common query examples

Find hardcoded credentials or private keys across all repositories:
(-----BEGIN [A-Z ]*PRIVATE KEY------)|("gh|'gh)[pousr]_[A-Za-z0-9_]{16,} patternType:regexp case:yes
Find all TODOs outside of JSON, Markdown, and text files:
-file:\.(json|md|txt)$ TODO|FIXME|HACK|XXX
Find recent changes to authentication code across all branches:
type:diff repo:^github\.com/myorg/ repo:@*refs/heads/ after:"1 week ago" \b(login|logout|auth|token|session)\b
Find all repositories that have a Dockerfile and use Python:
repo:has.path(Dockerfile) lang:python select:repo
Find dependency changes in the last week:
file:package\.json type:diff after:"1 week ago"

Overview

Introduction to Code Search concepts and search types.

Features

Saved searches, search contexts, code monitoring, and more.

Build docs developers (and LLMs) love