|
| 1 | +--- |
| 2 | +title: "Git" |
| 3 | +sidebarTitle: Overview |
| 4 | +--- |
| 5 | + |
| 6 | +Use the `sandbox.git` helper to run common git operations inside a sandbox. The helper is non-interactive, so pass credentials explicitly or store them. |
| 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 | +All examples below assume you already created a `sandbox` instance. |
| 13 | + |
| 14 | +## Clone a repository |
| 15 | + |
| 16 | +<CodeGroup> |
| 17 | +```js JavaScript & TypeScript |
| 18 | +import { Sandbox } from '@e2b/code-interpreter' |
| 19 | + |
| 20 | +const sandbox = await Sandbox.create() |
| 21 | + |
| 22 | +await sandbox.git.clone('https://git.example.com/org/repo.git', { |
| 23 | + path: '/workspace/repo', |
| 24 | + branch: 'main', |
| 25 | + depth: 1, |
| 26 | +}) |
| 27 | +``` |
| 28 | +```python Python |
| 29 | +from e2b_code_interpreter import Sandbox |
| 30 | + |
| 31 | +sandbox = Sandbox.create() |
| 32 | + |
| 33 | +sandbox.git.clone( |
| 34 | + 'https://git.example.com/org/repo.git', |
| 35 | + path='/workspace/repo', |
| 36 | + branch='main', |
| 37 | + depth=1, |
| 38 | +) |
| 39 | +``` |
| 40 | +</CodeGroup> |
| 41 | + |
| 42 | +## Check status and branches |
| 43 | + |
| 44 | +`status()` returns a structured object with branch, ahead/behind, and file status details. `branches()` returns the branch list and the current branch. |
| 45 | + |
| 46 | +<CodeGroup> |
| 47 | +```js JavaScript & TypeScript |
| 48 | +const status = await sandbox.git.status('/workspace/repo') |
| 49 | +console.log(status.currentBranch, status.ahead, status.behind) |
| 50 | +console.log(status.fileStatus) |
| 51 | + |
| 52 | +const branches = await sandbox.git.branches('/workspace/repo') |
| 53 | +console.log(branches.currentBranch) |
| 54 | +console.log(branches.branches) |
| 55 | +``` |
| 56 | +```python Python |
| 57 | +status = sandbox.git.status('/workspace/repo') |
| 58 | +print(status.current_branch, status.ahead, status.behind) |
| 59 | +print(status.file_status) |
| 60 | + |
| 61 | +branches = sandbox.git.branches('/workspace/repo') |
| 62 | +print(branches.current_branch) |
| 63 | +print(branches.branches) |
| 64 | +``` |
| 65 | +</CodeGroup> |
| 66 | + |
| 67 | +## Create and manage branches |
| 68 | + |
| 69 | +<CodeGroup> |
| 70 | +```js JavaScript & TypeScript |
| 71 | +await sandbox.git.createBranch('/workspace/repo', 'feature/new-docs') |
| 72 | +await sandbox.git.checkoutBranch('/workspace/repo', 'main') |
| 73 | +await sandbox.git.deleteBranch('/workspace/repo', 'feature/old-docs', { force: true }) |
| 74 | +``` |
| 75 | +```python Python |
| 76 | +sandbox.git.create_branch('/workspace/repo', 'feature/new-docs') |
| 77 | +sandbox.git.checkout_branch('/workspace/repo', 'main') |
| 78 | +sandbox.git.delete_branch('/workspace/repo', 'feature/old-docs', force=True) |
| 79 | +``` |
| 80 | +</CodeGroup> |
| 81 | + |
| 82 | +## Stage and commit |
| 83 | + |
| 84 | +<CodeGroup> |
| 85 | +```js JavaScript & TypeScript |
| 86 | +await sandbox.git.add('/workspace/repo', { all: true }) |
| 87 | +await sandbox.git.commit('/workspace/repo', 'Initial commit', { |
| 88 | + authorName: 'E2B Bot', |
| 89 | + authorEmail: 'bot@example.com', |
| 90 | +}) |
| 91 | +``` |
| 92 | +```python Python |
| 93 | +sandbox.git.add('/workspace/repo', all=True) |
| 94 | +sandbox.git.commit( |
| 95 | + '/workspace/repo', |
| 96 | + 'Initial commit', |
| 97 | + author_name='E2B Bot', |
| 98 | + author_email='bot@example.com', |
| 99 | +) |
| 100 | +``` |
| 101 | +</CodeGroup> |
| 102 | + |
| 103 | +## Pull and push (with credentials) |
| 104 | + |
| 105 | +For private repos over HTTP(S), pass `username` and `password` (token) to the command that needs it. A username is required whenever you pass a password/token. |
| 106 | + |
| 107 | +<CodeGroup> |
| 108 | +```js JavaScript & TypeScript |
| 109 | +await sandbox.git.push('/workspace/repo', { |
| 110 | + remote: 'origin', |
| 111 | + branch: 'main', |
| 112 | + setUpstream: true, |
| 113 | + username: process.env.GIT_USERNAME, |
| 114 | + password: process.env.GIT_TOKEN, |
| 115 | +}) |
| 116 | + |
| 117 | +await sandbox.git.pull('/workspace/repo', { |
| 118 | + remote: 'origin', |
| 119 | + branch: 'main', |
| 120 | + username: process.env.GIT_USERNAME, |
| 121 | + password: process.env.GIT_TOKEN, |
| 122 | +}) |
| 123 | +``` |
| 124 | +```python Python |
| 125 | +import os |
| 126 | + |
| 127 | +sandbox.git.push( |
| 128 | + '/workspace/repo', |
| 129 | + remote='origin', |
| 130 | + branch='main', |
| 131 | + set_upstream=True, |
| 132 | + username=os.environ.get('GIT_USERNAME'), |
| 133 | + password=os.environ.get('GIT_TOKEN'), |
| 134 | +) |
| 135 | + |
| 136 | +sandbox.git.pull( |
| 137 | + '/workspace/repo', |
| 138 | + remote='origin', |
| 139 | + branch='main', |
| 140 | + username=os.environ.get('GIT_USERNAME'), |
| 141 | + password=os.environ.get('GIT_TOKEN'), |
| 142 | +) |
| 143 | +``` |
| 144 | +</CodeGroup> |
| 145 | + |
| 146 | +## Manage remotes |
| 147 | + |
| 148 | +<CodeGroup> |
| 149 | +```js JavaScript & TypeScript |
| 150 | +await sandbox.git.remoteAdd('/workspace/repo', 'origin', 'https://git.example.com/org/repo.git', { |
| 151 | + fetch: true, |
| 152 | + overwrite: true, |
| 153 | +}) |
| 154 | +``` |
| 155 | +```python Python |
| 156 | +sandbox.git.remote_add( |
| 157 | + '/workspace/repo', |
| 158 | + 'origin', |
| 159 | + 'https://git.example.com/org/repo.git', |
| 160 | + fetch=True, |
| 161 | + overwrite=True, |
| 162 | +) |
| 163 | +``` |
| 164 | +</CodeGroup> |
| 165 | + |
| 166 | +## Authenticate once (credential helper) |
| 167 | + |
| 168 | +If you want to avoid passing credentials on each command, you can store them in the git credential helper inside the sandbox. |
| 169 | + |
| 170 | +<Warning> |
| 171 | +`dangerouslyAuthenticate()` / `dangerously_authenticate()` stores credentials on disk inside the sandbox. Any process or agent with access to the sandbox can read them. Use only when you understand the risk. |
| 172 | +</Warning> |
| 173 | + |
| 174 | +<CodeGroup> |
| 175 | +```js JavaScript & TypeScript |
| 176 | +await sandbox.git.dangerouslyAuthenticate({ |
| 177 | + username: process.env.GIT_USERNAME, |
| 178 | + password: process.env.GIT_TOKEN, |
| 179 | + host: 'git.example.com', |
| 180 | + protocol: 'https', |
| 181 | +}) |
| 182 | + |
| 183 | +// After this, HTTPS git operations can use the stored credentials. |
| 184 | +await sandbox.git.clone('https://git.example.com/org/private-repo.git', { |
| 185 | + path: '/workspace/repo', |
| 186 | +}) |
| 187 | +await sandbox.git.push('/workspace/repo', { remote: 'origin', branch: 'main' }) |
| 188 | +``` |
| 189 | +```python Python |
| 190 | +import os |
| 191 | + |
| 192 | +sandbox.git.dangerously_authenticate( |
| 193 | + username=os.environ.get('GIT_USERNAME'), |
| 194 | + password=os.environ.get('GIT_TOKEN'), |
| 195 | + host='git.example.com', |
| 196 | + protocol='https', |
| 197 | +) |
| 198 | + |
| 199 | +# After this, HTTPS git operations can use the stored credentials. |
| 200 | +sandbox.git.clone('https://git.example.com/org/private-repo.git', path='/workspace/repo') |
| 201 | +sandbox.git.push('/workspace/repo', remote='origin', branch='main') |
| 202 | +``` |
| 203 | +</CodeGroup> |
| 204 | + |
| 205 | +## Keep credentials in the remote URL |
| 206 | + |
| 207 | +If you intentionally want to keep credentials in the cloned repository, set the `dangerouslyStoreCredentials` / `dangerously_store_credentials` flag. |
| 208 | + |
| 209 | +<Warning> |
| 210 | +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. |
| 211 | +</Warning> |
| 212 | + |
| 213 | +<CodeGroup> |
| 214 | +```js JavaScript & TypeScript |
| 215 | +await sandbox.git.clone('https://git.example.com/org/private-repo.git', { |
| 216 | + username: process.env.GIT_USERNAME, |
| 217 | + password: process.env.GIT_TOKEN, |
| 218 | + dangerouslyStoreCredentials: true, |
| 219 | +}) |
| 220 | +``` |
| 221 | +```python Python |
| 222 | +import os |
| 223 | + |
| 224 | +sandbox.git.clone( |
| 225 | + 'https://git.example.com/org/private-repo.git', |
| 226 | + username=os.environ.get('GIT_USERNAME'), |
| 227 | + password=os.environ.get('GIT_TOKEN'), |
| 228 | + dangerously_store_credentials=True, |
| 229 | +) |
| 230 | +``` |
| 231 | +</CodeGroup> |
| 232 | + |
| 233 | +## Configure identity and config |
| 234 | + |
| 235 | +Set the global git author and other config values as needed. |
| 236 | + |
| 237 | +<CodeGroup> |
| 238 | +```js JavaScript & TypeScript |
| 239 | +await sandbox.git.configureUser('E2B Bot', 'bot@example.com') |
| 240 | +await sandbox.git.configSet('pull.rebase', 'false') |
| 241 | +const rebase = await sandbox.git.configGet('pull.rebase') |
| 242 | +``` |
| 243 | +```python Python |
| 244 | +sandbox.git.configure_user('E2B Bot', 'bot@example.com') |
| 245 | +sandbox.git.config_set('pull.rebase', 'false') |
| 246 | +rebase = sandbox.git.config_get('pull.rebase') |
| 247 | +``` |
| 248 | +</CodeGroup> |
0 commit comments