跪拜 Guibai
← Back to the summary

Git Worktrees Are the Isolation Layer AI Coding Agents Need

image.png Recently, many friends like me have been using Codex and Claude Desktop to write code on both macOS and Windows simultaneously, and we've been baffled by something called worktree.

You clearly told the AI to modify the code, but refreshing the page shows no effect at all? Chances are you've unknowingly fallen into the worktree "trap."

This article, based on my real-world troubleshooting experience, will help you thoroughly understand what Git worktree is, why it has suddenly become popular in the AI programming era, and how exactly to use it.

1. Why Traditional Branch Switching Is No Longer Enough

Let's first recall our usual development workflow. In a regular Git repository, you can only be on one branch at a time within a single directory:

This approach works fine for small projects. But in the AI programming era, pain points emerge:

If you keep switching branches in the same directory, not only is it easy to mess up uncommitted code, but the AI's context will also get cross-contaminated, wasting precious token quotas.

Worktree was born to solve this problem.

2. Understanding Worktree in One Sentence

Worktree = Multiple "working directories" from the same Git repository, doing different tasks simultaneously.

Git allows one repository to be associated with multiple working directories. This means you can place different branches of the same repository into different folders.

For example, if you have a main project my-project and now want to develop a login feature and fix a bug simultaneously, your directory structure with worktree would look like this:

They look like three independent project directories, but underneath they share the same Git repository data (commit history, object database, etc.).

3. Core Advantages of Worktree

Compared to cloning multiple repositories or frequently switching branches, worktree has three major benefits:

  1. No more frequent branch switching

    No more stash this and stash that. Want to switch branches? Just change folders. Each directory independently has its own HEAD, staging area, and working directory.

  2. Run multiple versions simultaneously

    For example, you can run npm run dev in the main directory to debug the main branch, while running a service on another port in a worktree directory to test a new feature, with no interference between the two.

  3. Perfectly suited for AI programming tools

    This is the most critical point! Tools like Claude Code can achieve perfect task isolation using worktree:

    • Each AI session gets its own exclusive worktree
    • If the AI messes up, just delete the worktree; the main directory code remains untouched
    • If the changes are good, merge them back in — safe and efficient

4. Hands-On Guide: From Creation to Cleanup

Don't be intimidated by the concept; the actual operation is very simple.

1. View existing worktrees

git worktree list

This lists all working trees and their statuses for the current repository.

2. Create a new worktree

git worktree add ../my-project-login feature/login

This creates a my-project-login folder in the parent directory and checks out the feature/login branch.

git worktree add -b feature/payments ../payments main

Creates a feature/payments branch based on main and checks it out in the new ../payments directory.

git worktree add -d ../debug-dir

This creates a "detached HEAD" working tree at the current commit, perfect for temporary testing.

3. Delete a worktree

Remember to clean up after use!

git worktree remove ../my-project-login

If you've already manually deleted the directory, use this command to clean up residual metadata records:

git worktree prune

5. Worktree Pitfall Avoidance Guide for AI Tools

Back to my initial confusion: why didn't code changes take effect in Claude Desktop?

Because the Claude Code desktop version automatically creates a worktree for each new session by default! And it usually places the worktree in the .claude/worktrees/ folder under the current project root directory.

If you don't notice this, start the service in the main directory, but let the AI modify code in the worktree directory, then refreshing the page ten thousand times won't show any changes.

Advanced Tips:

6. Summary

Once you understand worktree, you can not only avoid the basic mistake of "code changes not taking effect," but also more efficiently orchestrate multiple agents working in parallel in the AI programming era. Give it a try in your next project!