Search Examples

Learn and walkthrough some of the handy Code Search examples.

Sourcegraph Search Examples on GitHub Pages

Check out the Sourcegraph Search Examples site for filterable search examples with links to results on sourcegraph.com.

Below are some additional examples that search repositories on Sourcegraph.com, our open source code search solution for GitHub and GitLab. You can copy and adapt the following search queries for use on your company’s private instance.

See search query syntax reference.

Search through all the repositories in an organization

SGQUERY
context:global r:hashicorp/ terraform

Search a subset of repositories in an organization by language

SGQUERY
context:global r:hashicorp/vault* lang:yaml terraform

Search for one term or another in a specific repository

SGQUERY
context:global r:hashicorp/vault$ print( OR log(

Find private keys and GitHub access tokens checked in to code

SGQUERY
(-----BEGIN [A-Z ]*PRIVATE KEY------)|(("gh|'gh)[pousr]_[A-Za-z0-9_]{16,}) patternType:regexp case:yes

Recent security-related changes on all branches

SGQUERY
type:diff repo:@*refs/heads/ after:"5 days ago" \b(auth[^o][^r]|security\b|cve|password|secure|unsafe|perms|permissions)

Admitted hacks and TODOs in app code

SGQUERY
-file:\.(json|md|txt)$ hack|todo|kludge|fixme

Removal of TODOs in the repository commit log

SGQUERY
repo:^github\.com/sourcegraph/sourcegraph$ type:diff select:commit.diff.removed TODO

New usages of a function

SGQUERY
type:diff after:"1 week ago" \.subscribe\( lang:typescript

Find multiple terms in the same file, like testing of HTTP components

SGQUERY
repo:github\.com/sourcegraph/sourcegraph$ (test AND http AND NewRequest) lang:go

Recent quality related changes on all branches (customize for your linters)

SGQUERY
repo:@*refs/heads/:^master type:diff after:"1 week ago" (eslint-disable)

Recent dependency changes

SGQUERY
file:package.json type:diff after:"1 week ago"

Files that are Apache licensed

SGQUERY
licensed to the apache software foundation select:file

Only repositories with recent dependency changes

SGQUERY
file:package.json type:diff after:"1 week ago" select:repo

Search changes in a files that contain a keyword

SGQUERY
repo:^github\.com/sourcegraph/sourcegraph$ type:diff file:contains.content("golang\.org/x/sync/errgroup") .Go

When to use regex search mode

Sourcegraph's default literal search mode is line-based and will not match across lines, so regex can be useful when you wish to do so:

Matching multiple text strings in a file

SGQUERY
repo:^github\.com/Parsely/pykafka$ Not leader for partition

Regex searches are also useful when searching boundaries that are not delimited by code structures:

Finding css classes with word boundary regex

SGQUERY
repo:^github\.com/sourcegraph/ \bbtn-secondary\b

More examples of regex searches are available here

Previous
Search Filters Panel