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

# Git

Clone a repository, inspect changes, commit work, push a branch, or open a pull request from inside a box.

***

## Configure Git Access

If you want to work with private repositories, push changes, or create pull requests, create the box with a GitHub token.

<Note>
  For a fine-grained token, the following permissions are sufficient for basic usage:

  * **Contents** — Read and write
  * **Pull requests** — Read and write
</Note>

```bash title=".env" {2} theme={"system"}
UPSTASH_BOX_API_KEY=box_xxxxxxxxxxxxxxxxxxxxxxxx
GITHUB_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxxxxxx
```

```tsx {5-7} theme={"system"}
import { Agent, Box } from "@upstash/box"

const box = await Box.create({
  runtime: "node",
  git: {
    token: process.env.GITHUB_TOKEN,
  },
})
```

### Git identity

You can also set the git author identity that will be used for commits made inside the box. Both fields are optional and applied to the container's global git config on creation.

| Field       | Type     | Default             | Description                              |
| ----------- | -------- | ------------------- | ---------------------------------------- |
| `token`     | `string` | —                   | GitHub token for private repos and push  |
| `userName`  | `string` | `"Upstash Box"`     | Value written to `git config user.name`  |
| `userEmail` | `string` | `"box@upstash.com"` | Value written to `git config user.email` |

```tsx {5-9} theme={"system"}
import { Agent, Box } from "@upstash/box"

const box = await Box.create({
  runtime: "node",
  git: {
    token: process.env.GITHUB_TOKEN,
    userName: "deploy-bot",
    userEmail: "deploy-bot@acme.com",
  },
})
```

If `userName` or `userEmail` are omitted, the box uses the defaults (`"Upstash Box"` / `"box@upstash.com"`). Omitting `token` is fine for public repositories where push access is not required.

***

## Quickstart

### Clone a repository

```tsx theme={"system"}
await box.git.clone({
  repo: "https://github.com/acme/web-app",
  branch: "main",
})

await box.cd("web-app")

await box.git.exec({
  args: ["checkout", "-b", "fix/empty-state"],
})
```

### Inspect what changed

Use `status()` for a compact summary and `diff()` to diff the repo's uncommitted changes against the initial state.

```tsx theme={"system"}
const status = await box.git.status()
const diff = await box.git.diff()

console.log(status)
console.log(diff)
```

### Commit and push

After your code changes are ready, create a commit and push the branch.

```tsx theme={"system"}
const commit = await box.git.commit({
  message: "fix: handle empty state in dashboard",
})

await box.git.push({ branch: "fix/empty-state" })

console.log(commit.sha)
console.log(commit.message)
```

### Open a pull request

Create a PR once your branch is pushed.

```tsx theme={"system"}
const pr = await box.git.createPR({
  title: "fix: handle empty state in dashboard",
  body: "Improves the dashboard empty state and avoids a broken loading flow.",
  base: "main",
})

console.log(pr.url)
```

***

## API

### Clone

Clones a repository into the current working directory in the box.

```tsx theme={"system"}
await box.git.clone({
  repo: "https://github.com/acme/api",
})

// 👇 you can also select a branch during clone:
await box.git.clone({
  repo: "https://github.com/acme/api",
  branch: "develop",
})
```

### Status

Returns the Git status output for the repository in the current working directory.

* See what files changed
* See if there are there untracked files

```tsx theme={"system"}
const status = await box.git.status()
```

### Diff

Returns the current Git diff as a string.

* Useful to display a patch in your UI
* Review what an agent changed

```tsx theme={"system"}
const diff = await box.git.diff()
```

### Commit

Creates a commit and returns commit information including the SHA and message.

```tsx theme={"system"}
const commit = await box.git.commit({
  message: "feat: add onboarding checklist",
})
```

You can optionally override the commit author for a single commit using `authorName` and `authorEmail`. When omitted, the box's configured git identity is used (either the values set at creation via `config.git` or the latest `updateConfig` values).

```tsx theme={"system"}
const commit = await box.git.commit({
  message: "feat: add onboarding checklist",
  authorName: "Alice",
  authorEmail: "alice@acme.com",
})
```

| Option        | Type     | Required | Description                                    |
| ------------- | -------- | -------- | ---------------------------------------------- |
| `message`     | `string` | Yes      | Commit message                                 |
| `authorName`  | `string` | No       | Override `--author` name for this commit only  |
| `authorEmail` | `string` | No       | Override `--author` email for this commit only |

### Push

Pushes the current branch. You can also provide a branch name explicitly.

```tsx theme={"system"}
// 👇 push to default branch
await box.git.push()

// 👇 or to a specific branch
await box.git.push({
  branch: "feature/onboarding-checklist",
})
```

### Create a PR

Creates a pull request and returns the PR URL and metadata.

```tsx theme={"system"}
const pr = await box.git.createPR({
  title: "feat: add onboarding checklist",
  body: "Adds a simple onboarding checklist to improve first-run guidance.",
  base: "main",
})
```

### Checkout

Switches to another branch in the current repository.

```tsx theme={"system"}
await box.git.checkout({
  branch: "release/v1.2.0",
})
```

### Run a custom Git command

Runs a raw Git command and returns the command output. Useful escape hatch if the built-in helpers don't cover a use case.

```tsx theme={"system"}
const result = await box.git.exec({
  args: ["branch", "--show-current"],
})
```

### Update Git Config

Updates the git author identity in the running container. At least one field must be provided; the other field retains its current value.

Returns the effective identity values after the update.

```tsx theme={"system"}
const identity = await box.git.updateConfig({
  userName: "ci-bot",
  userEmail: "ci-bot@acme.com",
})

console.log(identity.git_user_name)  // "ci-bot"
console.log(identity.git_user_email) // "ci-bot@acme.com"
```

You can update a single field — the other will not change:

```tsx theme={"system"}
// Only update the email, keep the existing user name
const identity = await box.git.updateConfig({
  userEmail: "ci-bot@acme.com",
})
```

If the box is in Running or Idle state, the new config is applied immediately inside the container (equivalent to running `git config --global` for the updated fields). Changes take effect for all subsequent commits.

| Option      | Type     | Required | Description                |
| ----------- | -------- | -------- | -------------------------- |
| `userName`  | `string` | No       | New value for `user.name`  |
| `userEmail` | `string` | No       | New value for `user.email` |

**Return value:** `{ git_user_name: string; git_user_email: string }`

***

## Examples

### Use Git with an agent

If you configured a box agent, it also has full git access.

This is especially useful when the exact git steps are not known ahead of time. For example, if the user sends an open-ended request, you may not know in advance what branch name, commit message, or final push flow makes sense.

In that case, you can let the agent inspect the repository, decide what changes are needed, and handle the git workflow itself.

```tsx theme={"system"}
import { Agent, Box } from "@upstash/box"

const box = await Box.create({
  runtime: "node",
  agent: {
    harness: Agent.ClaudeCode,
    model: "anthropic/claude-opus-4-6",
    apiKey: process.env.ANTHROPIC_API_KEY,
  },
  git: {
    token: process.env.GITHUB_TOKEN,
  },
})

await box.git.clone({
  repo: "https://github.com/acme/web-app",
  branch: "main",
})

await box.cd("web-app")

const run = await box.agent.run({
  prompt: `
Inspect this repository and fix the broken mobile navigation.

Create a branch with a sensible name, make the necessary code and test changes, commit the changes, then push the branch and open a pull request against main.`,
})
```

### End-to-end PR automation

```tsx theme={"system"}
import { Agent, Box } from "@upstash/box"

const box = await Box.create({
  runtime: "node",
  agent: {
    harness: Agent.ClaudeCode,
    model: "anthropic/claude-opus-4-6",
    apiKey: process.env.ANTHROPIC_API_KEY,
  },
  git: {
    token: process.env.GITHUB_TOKEN,
  },
})

await box.git.clone({
  repo: "https://github.com/acme/docs-site",
  branch: "main",
})

await box.cd("docs-site")

await box.git.exec({
  args: ["checkout", "-b", "docs/deployment-troubleshooting"],
})

await box.agent.run({
  prompt: "Add a troubleshooting section to the deployment guide.",
})

await box.git.commit({
  message: "docs: add deployment troubleshooting section",
})

await box.git.push({ branch: "docs/deployment-troubleshooting" })

const pr = await box.git.createPR({
  title: "docs: add deployment troubleshooting section",
  base: "main",
})
```
