Work with Git
This guide shows you how to clone repositories, manage branches, and automate Git operations in the sandbox.
import { getSandbox } from "@cloudflare/sandbox";
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
// Basic cloneawait sandbox.gitCheckout("https://github.com/user/repo");
// Clone specific branchawait sandbox.gitCheckout("https://github.com/user/repo", { branch: "develop",});
// Shallow clone (faster for large repos)await sandbox.gitCheckout("https://github.com/user/large-repo", { depth: 1,});
// Clone to specific directoryawait sandbox.gitCheckout("https://github.com/user/my-app", { targetDir: "/workspace/project",});
import { getSandbox } from '@cloudflare/sandbox';
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
// Basic cloneawait sandbox.gitCheckout('https://github.com/user/repo');
// Clone specific branchawait sandbox.gitCheckout('https://github.com/user/repo', { branch: 'develop'});
// Shallow clone (faster for large repos)await sandbox.gitCheckout('https://github.com/user/large-repo', { depth: 1});
// Clone to specific directoryawait sandbox.gitCheckout('https://github.com/user/my-app', { targetDir: '/workspace/project'});
Use a personal access token in the URL:
const token = env.GITHUB_TOKEN;const repoUrl = `https://${token}@github.com/user/private-repo.git`;
await sandbox.gitCheckout(repoUrl);
const token = env.GITHUB_TOKEN;const repoUrl = `https://${token}@github.com/user/private-repo.git`;
await sandbox.gitCheckout(repoUrl);
Clone a repository and run build steps:
await sandbox.gitCheckout("https://github.com/user/my-app");
const repoName = "my-app";
// Install and buildawait sandbox.exec(`cd ${repoName} && npm install`);await sandbox.exec(`cd ${repoName} && npm run build`);
console.log("Build complete");
await sandbox.gitCheckout('https://github.com/user/my-app');
const repoName = 'my-app';
// Install and buildawait sandbox.exec(`cd ${repoName} && npm install`);await sandbox.exec(`cd ${repoName} && npm run build`);
console.log('Build complete');
await sandbox.gitCheckout("https://github.com/user/repo");
// Switch branchesawait sandbox.exec("cd repo && git checkout feature-branch");
// Create new branchawait sandbox.exec("cd repo && git checkout -b new-feature");
await sandbox.gitCheckout('https://github.com/user/repo');
// Switch branchesawait sandbox.exec('cd repo && git checkout feature-branch');
// Create new branchawait sandbox.exec('cd repo && git checkout -b new-feature');
await sandbox.gitCheckout("https://github.com/user/repo");
// Modify a fileconst readme = await sandbox.readFile("/workspace/repo/README.md");await sandbox.writeFile( "/workspace/repo/README.md", readme.content + "\n\n## New Section",);
// Commit changesawait sandbox.exec('cd repo && git config user.name "Sandbox Bot"');await sandbox.exec('cd repo && git config user.email "bot@example.com"');await sandbox.exec("cd repo && git add README.md");await sandbox.exec('cd repo && git commit -m "Update README"');
await sandbox.gitCheckout('https://github.com/user/repo');
// Modify a fileconst readme = await sandbox.readFile('/workspace/repo/README.md');await sandbox.writeFile('/workspace/repo/README.md', readme.content + '\n\n## New Section');
// Commit changesawait sandbox.exec('cd repo && git config user.name "Sandbox Bot"');await sandbox.exec('cd repo && git config user.email "bot@example.com"');await sandbox.exec('cd repo && git add README.md');await sandbox.exec('cd repo && git commit -m "Update README"');
- Use shallow clones - Faster for large repos with
depth: 1
- Store credentials securely - Use environment variables for tokens
- Clean up - Delete unused repositories to save space
Verify your token is set:
if (!env.GITHUB_TOKEN) { throw new Error("GITHUB_TOKEN not configured");}
const repoUrl = `https://${env.GITHUB_TOKEN}@github.com/user/private-repo.git`;await sandbox.gitCheckout(repoUrl);
if (!env.GITHUB_TOKEN) { throw new Error('GITHUB_TOKEN not configured');}
const repoUrl = `https://${env.GITHUB_TOKEN}@github.com/user/private-repo.git`;await sandbox.gitCheckout(repoUrl);
Use shallow clone:
await sandbox.gitCheckout("https://github.com/user/large-repo", { depth: 1,});
await sandbox.gitCheckout('https://github.com/user/large-repo', { depth: 1});
- Files API reference - File operations after cloning
- Execute commands guide - Run git commands
- Manage files guide - Work with cloned files
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-