# OpenCode Worktrees: Run OpenCode in a Git Worktree

_OpenCode v2 supports worktrees natively. Start a session in a new worktree from the desktop app or the terminal UI, and set where worktrees go with the `worktree.directory` option._

## The Fix

**Desktop app.** When you start a new session, select **New worktree**. The composer shows the base branch, such as `from dev ▾`. Click it to pick another base, or choose **Use main checkout** to go back.

**Terminal UI.** Press `Ctrl+O` to open recent sessions and projects. Press `Right` on a project to list its worktrees, then `Ctrl+N` to create one. Press `Tab` in the name prompt to generate a name.

In the dialog that moves a session, `Ctrl+A` creates a worktree, `Ctrl+M` moves the session into the selected one, and `Ctrl+D` deletes a worktree.

**Plain Git.** You can also create the worktree yourself and start OpenCode in it. OpenCode treats every worktree of a repository as part of one project:

```bash
git worktree add -b agent/search ../repo-search main
opencode ../repo-search
```

## Where Worktrees Go

Without configuration, OpenCode creates worktrees in its server data directory under `worktree/`. Set a parent directory in `opencode.json`:

```jsonc
{
  "worktree": {
    "directory": "../worktrees"
  }
}
```

A relative path resolves against the project's main checkout. OpenCode appends the worktree name to it. Changing the setting does not move existing worktrees.

Git is the default backend. A server plugin can register its own worktree strategy with `create`, `remove` and `list` operations, and that strategy then becomes the default.

## Why Use One

A worktree gives each OpenCode session its own files and index, so two sessions cannot overwrite each other's edits. [Run parallel agents in OpenCode and Zed](/learn/how-to/run-parallel-agents-in-opencode-and-zed) shows the full setup, including background subagents. Each worktree still starts with tracked files only. Install dependencies and copy `.env` files in each one, as described in [Share node_modules across Git worktrees](/learn/answers/agents-and-git/node-modules-in-worktrees).

## How Treq Handles It

Treq does not list OpenCode among the agents it starts. Open a [terminal session](/docs/concepts/terminal-sessions) in a [Treq workspace](/docs/concepts/workspaces) and run `opencode` there. The terminal starts in the workspace directory, which already has its own working copy and branch under `.treq/workspaces/`, so you do not need an OpenCode worktree on top.

## Sources

- [OpenCode config: Worktrees](https://opencode.ai/docs/config/)
- [OpenCode TUI](https://opencode.ai/docs/cli/tui/) and [keybinds](https://opencode.ai/docs/cli/keybinds/)
- [OpenCode changelog](https://opencode.ai/changelog)

## Related

- [Cursor worktrees](./cursor-worktrees.md)
- [Codex worktrees](./codex-worktrees.md)
- [What are Git Worktrees?](/learn/concepts/git/git-worktrees)
