Skip to content
WorktreeWise
#opencode#ai-agents#sandboxing#git-worktree

OpenCode + worktrees

W

WorktreeWise Engineering Team

Updated Sep 20265 min read

TL;DR: The 30-Second Summary

OpenCode autonomous sessions execute shell scripts, run linters, and refactor code. Running OpenCode in a separate Git worktree gives you complete sandbox control, preventing unintended branch changes and dirty tree states.

The Real-Life Scenario

An engineering team deploys OpenCode to run automated TypeScript strictness migrations across 200 files. Running this in a dedicated worktree ensures other developers can pull latest changes without being blocked by ongoing codemod runs.

OpenCode enables powerful automated developer workflows. Because OpenCode operates autonomously by executing shell commands and making broad file modifications, isolating it within a Git worktree guarantees that any failures or incomplete passes are contained.

Containing Autonomous Agent Side Effects

Autonomous agents can execute arbitrary commands (`npm install`, `rm`, `sed`). If an agent acts errantly in your main checkout, reverting the damage can be complex.

What you see in the terminal:

terminal output
[OpenCode] Executed: rm -rf ./cache && npm run codemod
Error: SyntaxError in 18 files. Branch state: UNCLEAN
🔍

Under the Hood: Git Plumbing & Architecture

In a dedicated worktree, the entire directory is an ephemeral disposable workspace. You can reset or discard it without any risk to your primary working tree.

Quick Command Recipes

Copy and adapt these commands directly in your terminal:

Create OpenCode sandbox

Isolates OpenCode execution into a clean directory.

bash
git worktree add ../task-opencode -b agent/opencode-migration main

Run OpenCode session

Executes the migration within the isolated boundary.

bash
cd ../task-opencode && opencode --task 'Strict null checks migration'
TUTORIAL

Step-by-Step Practical Walkthrough

Follow these verified steps to safely resolve the issue and guarantee that your filesystem and Git references are in sync.

1

Create worktree

Branch off main for the automated task.

terminal
git worktree add ../opencode-task -b agent/opencode main
2

Run OpenCode

Let OpenCode run autonomously.

terminal
cd ../opencode-task && opencode run
3

Review diff

Inspect generated changes before accepting.

terminal
git diff main...agent/opencode

Edge Cases & Advanced Scenarios

Submodule updates

If the project uses submodules, run `git submodule update --init` inside the new worktree.

⚠️

Common Mistakes to Avoid

❌ Mistake: Running autonomous agents with sudo/admin rights in shared directories

Why it causes trouble: Can modify system files outside the repository.

What to do instead: Run agents with standard user privileges inside dedicated worktrees.

Verification Checklist

  • OpenCode completes without affecting active working tree
  • Generated commits are clean and reviewable
💡

Senior Engineering Tips

  • WorktreeWise shows live terminal output of running agent sessions side by side.

Key Takeaways

  • 01.OpenCode sessions should always be sandboxed in dedicated worktrees.
  • 02.Zero impact on human developers working on the same repo.
TOOL

How WorktreeWise Solves This Visually

WorktreeWise eliminates the manual friction and mental overhead of CLI flags. It displays real-time branch states, uncommitted modifications, active terminals, and lock statuses across all worktrees on a single visual dashboard.

WorktreeWise managing OpenCode agent sessions in isolated worktrees
WorktreeWise managing OpenCode agent sessions in isolated worktrees
W

WorktreeWise Engineering Publication

Written and curated by the core WorktreeWise team. We build developer tools that turn Git worktrees, workflows, and parallel AI coding agents into second nature.

Related AI Agents Guides

View all ai agents