Skip to main content

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:

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/<first six characters of the project ID>. Set a parent directory in opencode.json:

{
"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 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.

How Treq Handles It​

Treq does not list OpenCode among the agents it starts. Open a terminal session in a Treq workspace 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​