> ## Documentation Index
> Fetch the complete documentation index at: https://docs.styne.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# A git workspace

> Clone, branch, commit and push inside a sandbox, like on a laptop.

**You need** a Styne API key and the unmodified E2B SDK with `E2B_API_KEY` and `E2B_API_URL=https://api.styne.ai` set ([Switch from E2B](https://docs.styne.ai/e2b-drop-in.md)). The `base` template has git.

## Clone, change, commit

The first example needs no network and no account: it makes a bare repository inside the sandbox to act as `origin`, so you
can run the whole loop and see every step. Replace that repository with your own remote in the next section.

```python Python theme={"theme":"css-variables"}
from e2b import Sandbox

with Sandbox.create(template="base", timeout=600) as sbx:
    run = lambda cmd, **kw: sbx.commands.run(cmd, **kw).stdout.strip()
    run("git init --bare /root/origin.git")
    run("git clone /root/origin.git /root/work")
    run("git config user.name Agent && git config user.email agent@example.com", cwd="/root/work")
    sbx.files.write("/root/work/hello.txt", "hello from a sandbox\n")
    run("git add -A && git commit -m 'add hello' && git push origin HEAD:main", cwd="/root/work")
    print(run("git log --oneline main", cwd="/root/origin.git"))
```

```typescript TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sbx = await Sandbox.create('base', { timeoutMs: 600_000 })
try {
  const run = async (cmd: string, cwd?: string) => (await sbx.commands.run(cmd, { cwd })).stdout.trim()
  await run('git init --bare /root/origin.git')
  await run('git clone /root/origin.git /root/work')
  await run('git config user.name Agent && git config user.email agent@example.com', '/root/work')
  await sbx.files.write('/root/work/hello.txt', 'hello from a sandbox\n')
  await run("git add -A && git commit -m 'add hello' && git push origin HEAD:main", '/root/work')
  console.log(await run('git log --oneline main', '/root/origin.git'))
} finally {
  await sbx.kill()
}
```

The last line prints the commit that reached `origin`, with its own hash:

```text theme={"theme":"css-variables"}
<short hash> add hello
```

## Use your own repository and a token

Pass the token to the one command that needs it, as an environment variable of that command. It is never written into a
URL, a git config or a file in the sandbox, and it is gone when the command ends. This block is shown, not run by the
docs smoke: it needs your repository and your token.

```text theme={"theme":"css-variables"}
from e2b import Sandbox
import os

repo = "https://github.com/<owner>/<repo>.git"
token = {"GIT_TOKEN": os.environ["GITHUB_TOKEN"]}
helper = "-c credential.helper='!f() { echo username=x-access-token; echo password=$GIT_TOKEN; }; f'"

with Sandbox.create(template="base", timeout=900) as sbx:
    sbx.commands.run(f"git {helper} clone --depth 1 {repo} /root/work", envs=token)
    sbx.files.write("/root/work/NOTES.md", "work in progress\n")
    sbx.commands.run("git checkout -b agent/notes && git add -A && git commit -m notes",
                     cwd="/root/work")
    sbx.commands.run(f"git {helper} push origin agent/notes", cwd="/root/work", envs=token)
```

Give the token the least it needs: a fine-grained token for one repository with contents write access. Anything the
sandbox runs can read an environment variable of its own commands, so use a token you would be content to see leaked into
that sandbox, and revoke it when the task ends.

## Keep the workspace between tasks

A workspace on a paused sandbox is exactly where you left it: files, branches and uncommitted changes. To start many tasks
from one prepared clone, save it as a snapshot and start every task from the snapshot; see
[Timeouts, pause and resume](https://docs.styne.ai/pause-tiers.md) and [Evals and RL fan-out](https://docs.styne.ai/guide-fan-out.md).

## Things to know

* Clone with `--depth 1` for large repositories: it is quicker and uses less of the sandbox's disk (51 GiB on `base`).
* The default working directory of a command is `/root`, the home directory of the `base` template. Give `cwd` or an
  absolute path when it matters.
* Output of `git` goes to stderr for progress lines. `commands.run` returns both; a non-zero exit raises
  `CommandExitException` in Python and `CommandExitError` in TypeScript.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.