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.
Search through all the repositories in an organization
SGQUERYcontext:global r:hashicorp/ terraform
Search a subset of repositories in an organization by language
SGQUERYcontext:global r:hashicorp/vault* lang:yaml terraform
Search for one term or another in a specific repository
SGQUERYcontext: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
SGQUERYtype: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
SGQUERYrepo:^github\.com/sourcegraph/sourcegraph$ type:diff select:commit.diff.removed TODO
SGQUERYtype:diff after:"1 week ago" \.subscribe\( lang:typescript
Find multiple terms in the same file, like testing of HTTP components
SGQUERYrepo:github\.com/sourcegraph/sourcegraph$ (test AND http AND NewRequest) lang:go
Recent quality related changes on all branches (customize for your linters)
SGQUERYrepo:@*refs/heads/:^master type:diff after:"1 week ago" (eslint-disable)
SGQUERYfile:package.json type:diff after:"1 week ago"
Files that are Apache licensed
SGQUERYlicensed to the apache software foundation select:file
Only repositories with recent dependency changes
SGQUERYfile:package.json type:diff after:"1 week ago" select:repo
Search changes in a files that contain a keyword
SGQUERYrepo:^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
SGQUERYrepo:^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
SGQUERYrepo:^github\.com/sourcegraph/ \bbtn-secondary\b
More examples of regex searches are available here