Work with Git
Last updated Sep 30, 2026 Note
This page documents Sandbox SDK 0.x for existing applications. For new applications, refer to Run tests from a Git repository. To move an existing application to @cloudflare/sandbox 1.0, refer to Migrate from Sandbox SDK 0.x.
This guide shows you how to clone repositories, manage branches, and automate Git operations in the sandbox.
Clone repositories
import { getSandbox } from "@cloudflare/sandbox";
const sandbox = getSandbox(env.Sandbox, "my-sandbox");
// Basic clone
await sandbox.gitCheckout("https://github.com/user/repo");
// Clone specific branch
await 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 directory
await sandbox.gitCheckout("https://github.com/user/my-app", {
targetDir: "/workspace/project",
});
import { getSandbox } from '@cloudflare/sandbox';
const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
// Basic clone
await sandbox.gitCheckout('https://github.com/user/repo');
// Clone specific branch
await 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 directory
await sandbox.gitCheckout('https://github.com/user/my-app', {
targetDir: '/workspace/project'
});
Clone private repositories
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);
More secure alternative
Embedding a token in the URL passes the credential directly into the sandbox. For better access control, use an outbound handler that injects the real token at request time — the sandbox never holds the credential. Refer to Handle outbound traffic.
Clone and build
Clone a repository and run build steps:
await sandbox.gitCheckout("https://github.com/user/my-app");
const repoName = "my-app";
// Install and build
await 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 build
await sandbox.exec(`cd ${repoName} && npm install`);
await sandbox.exec(`cd ${repoName} && npm run build`);
console.log('Build complete');
Work with branches
await sandbox.gitCheckout("https://github.com/user/repo");
// Switch branches
await sandbox.exec("cd repo && git checkout feature-branch");
// Create new branch
await sandbox.exec("cd repo && git checkout -b new-feature");
await sandbox.gitCheckout('https://github.com/user/repo');
// Switch branches
await sandbox.exec('cd repo && git checkout feature-branch');
// Create new branch
await sandbox.exec('cd repo && git checkout -b new-feature');
Make changes and commit
await sandbox.gitCheckout("https://github.com/user/repo");
// Modify a file
const readme = await sandbox.readFile("/workspace/repo/README.md");
await sandbox.writeFile(
"/workspace/repo/README.md",
readme.content + "\n\n## New Section",
);
// Commit changes
await 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 file
const readme = await sandbox.readFile('/workspace/repo/README.md');
await sandbox.writeFile('/workspace/repo/README.md', readme.content + '\n\n## New Section');
// Commit changes
await 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"');
Best practices
- 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
Troubleshooting
Authentication fails
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);
Large repository timeout
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
});
Related resources
- Files API reference - File operations after cloning
- Execute commands guide - Run git commands
- Manage files guide - Work with cloned files
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/sandbox/sdk/guides/git-workflows/#page","headline":"Work with Git","description":"Clone repositories, manage branches, and automate Git operations with the Sandbox SDK 0.x.","url":"https://developers.cloudflare.com/sandbox/sdk/guides/git-workflows/","inLanguage":"en","image":"https://developers.cloudflare.com/sandbox/sdk/guides/git-workflows/og.png?v=048dfbf4df6a0a20","dateModified":"2026-09-30","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Read, write, organize, and synchronize files in a sandbox with the Sandbox SDK 0.x. title: Manage files image: https://developers.cloudflare.com/sandbox/sdk/guides/manage-files/og.png?v=2d65d3edba18b16f
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/sandbox/llms.txt
Use this file to discover all available pages before exploring further.
Docs