Skip to main content
Clones a Git repository into the sandbox filesystem, supporting custom branches, shallow clones, and authenticated repositories.

Method Signature

async gitCheckout(
  repoUrl: string,
  options?: {
    branch?: string;
    targetDir?: string;
    depth?: number;
  }
): Promise<GitCheckoutResult>

Parameters

repoUrl
string
required
URL of the Git repository to clone. Supports HTTPS and SSH URLs.
options
object
Clone options

Returns

GitCheckoutResult
object
Result of the git clone operation

Example

import { getSandbox } from '@cloudflare/sandbox';

const sandbox = getSandbox(env.SANDBOX, 'my-sandbox');

// Clone a public repository
const result = await sandbox.gitCheckout(
  'https://github.com/cloudflare/sandbox-sdk'
);

console.log('Cloned to:', result.targetDir); // /workspace/sandbox-sdk
console.log('Branch:', result.branch); // main

Example: Shallow Clone

// Clone only the latest commit for faster cloning
const result = await sandbox.gitCheckout(
  'https://github.com/large/repository',
  {
    depth: 1,
    targetDir: '/workspace/my-repo'
  }
);

Example: Specific Branch

const result = await sandbox.gitCheckout(
  'https://github.com/user/repo',
  {
    branch: 'develop',
    targetDir: '/workspace/dev'
  }
);

Example: Private Repository with Token

// Use personal access token in URL
const token = env.GITHUB_TOKEN;
const result = await sandbox.gitCheckout(
  `https://${token}@github.com/user/private-repo`
);

// Or set up SSH key beforehand
await sandbox.exec('mkdir -p ~/.ssh');
await sandbox.writeFile('~/.ssh/id_rsa', env.SSH_PRIVATE_KEY);
await sandbox.exec('chmod 600 ~/.ssh/id_rsa');

const result = await sandbox.gitCheckout(
  'git@github.com:user/private-repo.git'
);

Error Handling

Throws an error if:
  • Repository URL is invalid
  • Repository does not exist or is not accessible
  • Branch does not exist
  • Target directory already exists
  • Insufficient disk space
  • Authentication fails (for private repos)
try {
  await sandbox.gitCheckout('https://github.com/invalid/repo');
} catch (error) {
  console.error('Clone failed:', error.message);
}

Notes

  • Credentials in URLs are automatically redacted from logs
  • Target directory is created if it doesn’t exist
  • Cloning large repositories may take time - consider using depth: 1
  • SSH keys must be set up before cloning SSH URLs
  • Supports GitHub, GitLab, Bitbucket, and any Git server
  • Default branch is used when branch is not specified

See Also

  • exec - Run git commands directly
  • listFiles - List cloned repository files

Build docs developers (and LLMs) love