Site admins can sync Git repositories hosted on and GitHub Enterprise with Sourcegraph so that users can search and navigate the repositories.

To connect GitHub to Sourcegraph:

  1. Go to Site admin > Manage code hosts
  2. Select GitHub.
  3. Configure the connection to GitHub using the action buttons above the text field, and additional fields can be added using Cmd/Ctrl+Space for auto-completion. See the configuration documentation below.
  4. Press Add repositories.

In this example, the kubernetes public repository on GitHub is added by selecting Add a singe repository and replacing <owner>/<repository> with kubernetes/kubernetes:

  "url": "",
  "token": "<access token>",
  "orgs": [],
  "repos": [

Supported versions

  • GitHub Enterprise v2.10 and newer

There are four fields for configuring which repositories are mirrored/synchronized:

  • repos
    A list of repositories in owner/name format. The order determines the order in which we sync repository metadata and is safe to change.
  • orgs
    A list of organizations (every repository belonging to the organization will be cloned).
  • repositoryQuery
    A list of strings with three pre-defined options (public, affiliated, none, none of which are subject to result limitations), and/or a GitHub advanced search query. Note: There is an existing limitation that requires the latter, GitHub advanced search queries, to return less than 1000 results. See this issue for ongoing work to address this limitation.
  • exclude
    A list of repositories to exclude which takes precedence over the repos, orgs, and repositoryQuery fields.

Private repositories

A token that has the prerequisite scopes is required in order to clone private repositories for search, as well as at least read access to the relevant private repositories.

See GitHub API token and access for more details.

GitHub API token and access

The GitHub service requires a token in order to access their API. There are two different types of tokens you can supply:

  • Personal access token:
    This gives Sourcegraph the same level of access to repositories as the account that created the token. If you’re not wanting to mix your personal repositories with your organizations repositories, you could add an entry to the exclude array, or you can use a machine user token.
  • Machine user token:
    Generates a token for a machine user that is affiliated with an organization instead of a user account.

No token scopes are required if you only want to sync public repositories and don’t want to use any of the following features. Otherwise, the following token scopes are required for specific features:

Feature Required token scopes
Sync private repositories repo
Sync repository permissions repo
Repository permissions caching repo, write:org
Batch changes repo, read:org, user:email, read:discussion, and workflow (learn more)

Fine-grained personal access tokens

GitHub’s fine-grained personal access tokens are not yet supported. rate limits

You should always include a token in a configuration for a URL to avoid being denied service by GitHub’s unauthenticated rate limits. If you don’t want to automatically synchronize repositories from the account associated with your personal access token, you can create a token without a repo scope for the purposes of bypassing rate limit restrictions only.

GitHub Enterprise Server rate limits

Rate limiting may not be enabled by default. To check and verify the current rate limit settings, you may make a request to the /rate_limit endpoint like this:

$ curl -s https://<github-enterprise-url>/api/v3/rate_limit -H "Authorization: Bearer <token>"
  "message": "Rate limiting is not enabled.",
  "documentation_url": ""

Internal rate limits

Internal rate limiting can be configured to limit the rate at which requests are made from Sourcegraph to GitHub.

If enabled, the default rate is set at 5000 per hour which can be configured via the requestsPerHour field (see below):

  • For Sourcegraph <=3.38, if rate limiting is configured more than once for the same code host instance, the most restrictive limit will be used.
  • For Sourcegraph >=3.39, rate limiting should be enabled and configured for each individual code host connection.

NOTE Internal rate limiting is only currently applied when synchronising changesets in batch changes, repository permissions and repository metadata from code hosts.

Repository permissions

By default, all Sourcegraph users can view all repositories. To configure Sourcegraph to use GitHub’s per-user repository permissions, see “Repository permissions”.

User authentication

To configure GitHub as an authentication provider (which will enable sign-in via GitHub), see the authentication documentation.


Using the webhooks property on the external service has been deprecated.

Please consult this page in order to configure webhooks.


GitHub connections support the following configuration options, which are specified in the JSON editor in the site admin “Manage code hosts” area.


	// If non-null, enforces GitHub repository permissions. This requires that there is an item in the [site configuration json]( `auth.providers` field, of type "github" with the same `url` field as specified in this `GitHubConnection`.
	"authorization": null,

	// TLS certificate of the GitHub Enterprise instance. This is only necessary if the certificate is self-signed or signed by an internal CA. To get the certificate run `openssl s_client -connect HOST:443 -showcerts < /dev/null 2> /dev/null | openssl x509 -outform PEM`. To escape the value into a JSON string, you may want to use a tool like
	"certificate": null,
	// Other example values:
	// - "-----BEGIN CERTIFICATE-----\n..."

	// Only used to override the cloud_default column from a config file specified by EXTSVC_CONFIG_FILE
	"cloudDefault": false,

	// When set to true, this external service will be chosen as our 'Global' GitHub service. Only valid on Only one service can have this flag set.
	"cloudGlobal": false,

	// A list of repositories to never mirror from this GitHub instance. Takes precedence over "orgs", "repos", and "repositoryQuery" configuration.
	// Supports excluding by name ({"name": "owner/name"}) or by ID ({"id": "MDEwOlJlcG9zaXRvcnkxMTczMDM0Mg=="}).
	// Note: ID is the GitHub GraphQL ID, not the GitHub database ID. eg: "curl | jq .node_id"
	"exclude": null,
	// Other example values:
	// - [{"forks":true}]
	// - [
	//     {
	//       "name": "owner/name"
	//     },
	//     {
	//       "id": "MDEwOlJlcG9zaXRvcnkxMTczMDM0Mg=="
	//     }
	//   ]
	// - [
	//     {
	//       "name": "vuejs/vue"
	//     },
	//     {
	//       "name": "php/php-src"
	//     },
	//     {
	//       "pattern": "^topsecretorg/.*"
	//     }
	//   ]

	// The type of Git URLs to use for cloning and fetching Git repositories on this GitHub instance.
	// If "http", Sourcegraph will access GitHub repositories using Git URLs of the form http(s):// (using https: if the GitHub instance uses HTTPS).
	// If "ssh", Sourcegraph will access GitHub repositories using Git URLs of the form [email protected]:myteam/myproject.git. See the documentation for how to provide SSH private keys and known_hosts:
	"gitURLType": "http",

	// The installation ID of the GitHub App.
	"githubAppInstallationID": null,

	// Deprecated and ignored field which will be removed entirely in the next release. GitHub repositories can no longer be enabled or disabled explicitly. Configure repositories to be mirrored via "repos", "exclude" and "repositoryQuery" instead.
	"initialRepositoryEnablement": null,

	// An array of organization names identifying GitHub organizations whose repositories should be mirrored on Sourcegraph.
	"orgs": null,
	// Other example values:
	// - ["name"]
	// - [
	//     "kubernetes",
	//     "golang",
	//     "facebook"
	//   ]

	// Whether the code host connection is in a pending state.
	"pending": false,

	// Rate limit applied when making background API requests to GitHub.
	"rateLimit": {
		"enabled": true,
		"requestsPerHour": 5000

	// An array of repository "owner/name" strings specifying which GitHub or GitHub Enterprise repositories to mirror on Sourcegraph.
	"repos": null,
	// Other example values:
	// - ["owner/name"]
	// - [
	//     "kubernetes/kubernetes",
	//     "golang/go",
	//     "facebook/react"
	//   ]

	// The pattern used to generate the corresponding Sourcegraph repository name for a GitHub or GitHub Enterprise repository. In the pattern, the variable "{host}" is replaced with the GitHub host (such as, and "{nameWithOwner}" is replaced with the GitHub repository's "owner/path" (such as "myorg/myrepo").
	// For example, if your GitHub Enterprise URL is and your Sourcegraph URL is, then a repositoryPathPattern of "{host}/{nameWithOwner}" would mean that a GitHub repository at is available on Sourcegraph at
	// It is important that the Sourcegraph repository name generated with this pattern be unique to this code host. If different code hosts generate repository names that collide, Sourcegraph's behavior is undefined.
	"repositoryPathPattern": "{host}/{nameWithOwner}",

	// An array of strings specifying which GitHub or GitHub Enterprise repositories to mirror on Sourcegraph. The valid values are:
	// - `public` mirrors all public repositories for GitHub Enterprise and is the equivalent of `none` for GitHub
	// - `affiliated` mirrors all repositories affiliated with the configured token's user:
	// 	- Private repositories with read access
	// 	- Public repositories owned by the user or their orgs
	// 	- Public repositories with write access
	// - `none` mirrors no repositories (except those specified in the `repos` configuration property or added manually)
	// - All other values are executed as a GitHub advanced repository search as described at Example: to sync all repositories from the "sourcegraph" organization including forks the query would be "org:sourcegraph fork:true".
	// If multiple values are provided, their results are unioned.
	// If you need to narrow the set of mirrored repositories further (and don't want to enumerate it with a list or query set as above), create a new bot/machine user on GitHub or GitHub Enterprise that is only affiliated with the desired repositories.
	"repositoryQuery": [

	// A GitHub personal access token. Create one for at (for GitHub Enterprise, replace with your instance's hostname). See for which scopes are required for which use cases.
	"token": null,

	// URL of a GitHub instance, such as or
	"url": null,
	// Other example values:
	// - ""
	// - ""

	// An array of configurations defining existing GitHub webhooks that send updates back to Sourcegraph.
	"webhooks": null
	// Other example values:
	// - [
	//     {
	//       "org": "yourorgname",
	//       "secret": "webhook-secret"
	//     }
	//   ]

Default branch

Sourcegraph displays search results from the default branch of a repository when no revision: parameter is specified. If you’d like the search results to be displayed from another branch by default, you may change a repo’s default branch on the github repo settings page. If this is not an option, consider using search contexts instead.


Hitting GitHub Search API rate limit with repositoryQuery

When Sourcegraph syncs repositories configured via repositoryQuery, it consumes GitHub API search rate limit, which is lower than the normal rate limit. The affiliated, public and none special values, however, trigger normal API requests instead of search API requests.

When the search rate limit quota is exhausted, an error like failed to list GitHub repositories for search: page=..., searchString=\"...\" can be found in logs. To work around this try reducing the frequency with which repository syncing happens by setting a higher value (in minutes) of repoListUpdateInterval in your Sourcegraph site config.

repositoryQuery is the only repo syncing method that consumes GitHub search API quota, so if setting repoListUpdateInterval doesn’t work consider switching your syncing method to use another option, like orgs, or using one of the special values described above.

“repositoryQuery”: [“public”] does not return archived status of a repo

The repositoryQuery option "public" is valuable in that it allows sourcegraph to sync all public repositories, however, it does not return whether or not a repo is archived. This can result in archived repos appearing in normal search. You can see an example of what is returned by the GitHub API for a query to “public” here.

If you would like to sync all public repositories while omitting archived repos, consider generating a GitHub token with access to only public repositories, then use respositoryQuery with option affiliated and an exclude argument with option public as seen in the example below:

    "url": "",
    "gitURLType": "http",
    "repositoryPathPattern": "devs/{nameWithOwner}",
    "repositoryQuery": [
    "exclude": [
            "archived": true