The Deska blog

Letting Agents Work on Main vs Isolated Worktrees

Compare the safety strategies of letting agents work on main versus isolated worktrees to improve your development workflow and protect your codebase.

· 11 min read

Deciding whether to let AI coding agents work on main or isolated worktrees is a pivotal security and productivity choice for modern engineering teams. As agents like Claude Code or Codex CLI become more capable of autonomous edits, the risk profile changes. Directing an agent to operate on your main branch offers speed but introduces significant risks to repository stability. Conversely, using git worktrees provides a sandbox that prevents unverified changes from polluting your primary workspace before they are properly reviewed.

Understanding the risks of agents working on main

When you allow an agent to work on the main branch, you are granting it the highest level of trust possible in a git workflow. For small, personal projects or rapid prototyping, this might seem efficient. However, the lack of a buffer creates specific technical challenges that can derail a project quickly.

The most immediate concern is the pollution of the working directory. AI agents often generate intermediate files, logs, or temporary build artifacts that might not be properly ignored by your .gitignore file. If the agent operates directly on your primary branch, these files can easily be staged and committed by mistake. Furthermore, if an agent makes a logic error that breaks the build or introduces a regression, your entire development environment is compromised until you manually revert the changes. This leads to time lost in context switching and debugging the agent instead of building features.

Another issue is the lack of a clear audit trail. When an agent works on main, every small change or iteration might be mixed with your own manual edits. If you need to perform a git bisect later to find a bug, distinguishing between what you wrote and what the agent wrote becomes significantly harder. The history becomes a messy amalgam of human and machine output, reducing the long term maintainability of the codebase.

The advantages of isolated worktrees

Git worktrees allow you to have multiple branches checked out simultaneously in separate directories. This is fundamentally different from just switching branches. With worktrees, you can have your main branch open in one terminal while an agent stays contained in a separate directory dedicated to a specific task.

Isolation provides a physical and logical boundary. If an agent goes into a loop or generates thousands of lines of incorrect code, it is limited to that specific folder. Your main workspace remains clean. This allows for a much higher degree of parallel work. While the agent is busy refactoring a module in one worktree, you can continue fixing critical bugs in another without any file locks or build interference.

Safety is also enhanced through validation. Since the agent is in a worktree, you can run automated tests or linters in that specific directory before the code ever touches your primary workflow. This acts as a manual gate. Only after you have verified the agent's output do you merge or move that work into your main development stream. This strategy aligns with the principle of least privilege, giving the agent only the scope it needs to complete its task.

Choosing the right strategy for your project

The choice between these two approaches often depends on the complexity of the task and the maturity of the agent being used.

  1. High speed prototyping: If you are building a greenfield project where no users are active, letting agents work on main might be acceptable. The speed of iteration is the priority, and the cost of a broken build is low.
  2. Production codebases: For any project with active users or a team of developers, isolated worktrees are the standard. The risk of breaking the main branch is too high to justify the slight speed increase of direct editing.
  3. Complex refactoring: When an agent is tasked with sweeping changes across multiple files, worktrees allow you to inspect the "before and after" state side by side. This is nearly impossible to do effectively if the agent is overwriting your active workspace.
  4. Exploratory tasks: If you are asking an agent to research a library or try out a new implementation, doing so in a worktree ensures that if the experiment fails, you can simply delete the directory and move on.

Implementing agent isolation in your workflow

To start using worktrees with your coding agents, you need to familiarize yourself with the git worktree command. Instead of cloning the repo multiple times, you use the existing .git metadata to create a new linked working tree.

For example, you can create a worktree for an agent with git worktree add ../agent-task-1 feature-branch. You then point your agentic tool to that specific directory. Many command line agents accept a path argument. By restricting them to that path, you ensure they cannot affect files in your primary directory. This separation is the foundation of a robust AI augmented development cycle.

Reviewing the code in these worktrees is simpler. You can use a diff tool to compare the two directories or simply use your terminal to run a specialized test suite within the agent's folder. Once satisfied, you can commit from the worktree and push the branch for a formal pull request.

Using Deska for agent management

Deska provides a unique environment for managing these workflows because it is built around an infinite canvas workspace. Instead of juggling multiple terminal windows and IDE instances, you can place panels for your main branch and your agent worktrees side by side.

Because Deska runs AI coding agents like Claude Code, Codex CLI, and OpenCode as distinct panels, you can observe their progress in real time. You might have one terminal running a build on your main branch while another panel shows a coding agent working in an isolated worktree. The infinite canvas lets you zoom out to see the status of all active agents at once, providing a high level view of your different work streams.

Deska is a local-first application, meaning your code, files, and agent sessions stay on your machine. This is crucial when working with isolated worktrees because it ensures that the temporary files created by agents are not uploaded to a third party cloud. You maintain full control over your data and your repository structure.

If you need to move between environments, the mobile app allows you to monitor the progress of these agents through a secure relay. You can check if an agent has finished its task in a specific worktree while you are away from your desk, ensuring that the isolation strategy does not become a bottleneck for productivity.

Monitoring and driving the workspace

Managing multiple coding agents across different directories can become complex. This is where the Ask Deska assistant helps. You can use voice or chat to drive the workspace, asking it to open specific panels or check on the status of a long running command in a particular worktree.

If an agent has finished its work in an isolated directory, you can use Ask Deska to open the code editor (which uses Monaco) to review the changes. This integrated approach reduces the friction of context switching between different git worktrees. The ability to place notes and browser widgets next to your code panels ensures that you can document the agent's findings as they happen.

The workspace is free to use, and you can bring your own API keys if you prefer the lifetime tier. This flexibility allows you to experiment with both main branch and worktree strategies to see which fits your personal style best. For those who want everything set up immediately, managed inference is also available for subscribers.

Frequently Asked Questions

How to use git worktree with AI agents?

To use git worktrees with AI agents, create a new worktree directory using the git worktree add command. Once the directory is ready, launch your coding agent pointing its execution path to that specific folder. This ensures the agent only has access to the files within that isolated branch and cannot modify your primary working directory.

Can coding agents break my main branch?

Yes, if a coding agent is given direct write access to your main branch, it can introduce syntax errors, logic bugs, or delete important files. Because agents can work faster than humans, they can create many errors in a short period. Using isolated environments or strictly reviewing their commits before merging is the best way to prevent this.

What are the benefits of isolated worktrees for developers?

Isolated worktrees allow developers to work on multiple tasks or branches simultaneously without the need for constant stashing or switching. For those using AI, it provides a safe sandbox where agents can operate without interfering with the developer's active environment, making it easier to verify machine generated code before integration.

Get started with a safer workflow

If you are looking for a better way to organize your AI development tools and manage your repository safety, you can download the app at /download. Deska offers a flexible environment that supports various strategies, from direct edits on main for quick fixes to fully isolated worktree setups for complex engineering tasks. By providing the tools to visualize and control these agents in a local-first workspace, you can maximize the benefits of AI without compromising the integrity of your code.

💡 Ideas+🐛 BugsSuggest a feature or report a bug