|
| 1 | +--- |
| 2 | +title: "Clone a repository" |
| 3 | +sidebarTitle: Clone |
| 4 | +--- |
| 5 | + |
| 6 | +Use the `sandbox.git` helper to clone repositories into the sandbox. |
| 7 | + |
| 8 | +<Note> |
| 9 | +Git is available in the default sandbox template. If you’re using a custom or minimal template without git, install it first. See the [install custom packages](/docs/quickstart/install-custom-packages) guide or add git in your [template definition](/docs/template/defining-template). |
| 10 | +</Note> |
| 11 | + |
| 12 | +## Basic clone |
| 13 | + |
| 14 | +<CodeGroup> |
| 15 | +```js JavaScript & TypeScript |
| 16 | +import { Sandbox } from '@e2b/code-interpreter' |
| 17 | + |
| 18 | +const sandbox = await Sandbox.create() |
| 19 | + |
| 20 | +await sandbox.git.clone('https://github.com/org/repo.git', { |
| 21 | + path: '/workspace/repo', |
| 22 | + branch: 'main', |
| 23 | + depth: 1, |
| 24 | +}) |
| 25 | +``` |
| 26 | +```python Python |
| 27 | +from e2b_code_interpreter import Sandbox |
| 28 | + |
| 29 | +sandbox = Sandbox.create() |
| 30 | + |
| 31 | +sandbox.git.clone( |
| 32 | + 'https://github.com/org/repo.git', |
| 33 | + path='/workspace/repo', |
| 34 | + branch='main', |
| 35 | + depth=1, |
| 36 | +) |
| 37 | +``` |
| 38 | +</CodeGroup> |
| 39 | + |
| 40 | +## Clone with credentials |
| 41 | + |
| 42 | +For private repos over HTTP(S), pass `username` and `password` (token). By default, credentials are stripped from the remote URL after cloning. |
| 43 | + |
| 44 | +<CodeGroup> |
| 45 | +```js JavaScript & TypeScript |
| 46 | +await sandbox.git.clone('https://github.com/org/private-repo.git', { |
| 47 | + path: '/workspace/private-repo', |
| 48 | + username: process.env.GIT_USERNAME, |
| 49 | + password: process.env.GIT_TOKEN, |
| 50 | +}) |
| 51 | +``` |
| 52 | +```python Python |
| 53 | +import os |
| 54 | + |
| 55 | +sandbox.git.clone( |
| 56 | + 'https://github.com/org/private-repo.git', |
| 57 | + path='/workspace/private-repo', |
| 58 | + username=os.environ.get('GIT_USERNAME'), |
| 59 | + password=os.environ.get('GIT_TOKEN'), |
| 60 | +) |
| 61 | +``` |
| 62 | +</CodeGroup> |
| 63 | + |
| 64 | +## Keep credentials in the remote URL |
| 65 | + |
| 66 | +If you intentionally want to keep credentials in the cloned repository, set the `dangerouslyStoreCredentials` / `dangerously_store_credentials` flag. |
| 67 | + |
| 68 | +<Warning> |
| 69 | +Storing credentials in the repository remote persists them in the repo config. Any process or agent with access to the sandbox can read them. Only use this when required. |
| 70 | +</Warning> |
| 71 | + |
| 72 | +<CodeGroup> |
| 73 | +```js JavaScript & TypeScript |
| 74 | +await sandbox.git.clone('https://github.com/org/private-repo.git', { |
| 75 | + username: process.env.GIT_USERNAME, |
| 76 | + password: process.env.GIT_TOKEN, |
| 77 | + dangerouslyStoreCredentials: true, |
| 78 | +}) |
| 79 | +``` |
| 80 | +```python Python |
| 81 | +import os |
| 82 | + |
| 83 | +sandbox.git.clone( |
| 84 | + 'https://github.com/org/private-repo.git', |
| 85 | + username=os.environ.get('GIT_USERNAME'), |
| 86 | + password=os.environ.get('GIT_TOKEN'), |
| 87 | + dangerously_store_credentials=True, |
| 88 | +) |
| 89 | +``` |
| 90 | +</CodeGroup> |
| 91 | + |
| 92 | +## Status and branches |
| 93 | + |
| 94 | +`status()` returns a structured object with branch, ahead/behind, and file status details. `branches()` returns the branch list and the current branch. |
| 95 | + |
| 96 | +<CodeGroup> |
| 97 | +```js JavaScript & TypeScript |
| 98 | +const status = await sandbox.git.status('/workspace/repo') |
| 99 | +console.log(status.currentBranch, status.ahead, status.behind) |
| 100 | +console.log(status.fileStatus) |
| 101 | + |
| 102 | +const branches = await sandbox.git.branches('/workspace/repo') |
| 103 | +console.log(branches.currentBranch) |
| 104 | +console.log(branches.branches) |
| 105 | +``` |
| 106 | +```python Python |
| 107 | +status = sandbox.git.status('/workspace/repo') |
| 108 | +print(status.current_branch, status.ahead, status.behind) |
| 109 | +print(status.file_status) |
| 110 | + |
| 111 | +branches = sandbox.git.branches('/workspace/repo') |
| 112 | +print(branches.current_branch) |
| 113 | +print(branches.branches) |
| 114 | +``` |
| 115 | +</CodeGroup> |
| 116 | + |
| 117 | +## Manage branches |
| 118 | + |
| 119 | +<CodeGroup> |
| 120 | +```js JavaScript & TypeScript |
| 121 | +await sandbox.git.createBranch('/workspace/repo', 'feature/new-docs') |
| 122 | +await sandbox.git.checkoutBranch('/workspace/repo', 'main') |
| 123 | +await sandbox.git.deleteBranch('/workspace/repo', 'feature/old-docs', { force: true }) |
| 124 | +``` |
| 125 | +```python Python |
| 126 | +sandbox.git.create_branch('/workspace/repo', 'feature/new-docs') |
| 127 | +sandbox.git.checkout_branch('/workspace/repo', 'main') |
| 128 | +sandbox.git.delete_branch('/workspace/repo', 'feature/old-docs', force=True) |
| 129 | +``` |
| 130 | +</CodeGroup> |
| 131 | + |
| 132 | +## Stage and commit |
| 133 | + |
| 134 | +<CodeGroup> |
| 135 | +```js JavaScript & TypeScript |
| 136 | +await sandbox.git.add('/workspace/repo', { all: true }) |
| 137 | +await sandbox.git.commit('/workspace/repo', 'Initial commit', { |
| 138 | + authorName: 'E2B Bot', |
| 139 | + authorEmail: 'bot@example.com', |
| 140 | +}) |
| 141 | +``` |
| 142 | +```python Python |
| 143 | +sandbox.git.add('/workspace/repo', all=True) |
| 144 | +sandbox.git.commit( |
| 145 | + '/workspace/repo', |
| 146 | + 'Initial commit', |
| 147 | + author_name='E2B Bot', |
| 148 | + author_email='bot@example.com', |
| 149 | +) |
| 150 | +``` |
| 151 | +</CodeGroup> |
0 commit comments