If you use agentic coding tools — whether that is Claude Code, terminal coding assistants, or autonomous background IDE agents — you quickly encounter the single-working-tree bottleneck.
You ask an agent to refactor your database migrations and update tests. The agent starts analyzing files, editing classes, and executing test suites. For the next five minutes, your repository’s working directory is occupied:
- You cannot easily work on another feature because the agent is modifying unstaged files in place.
- You cannot switch branches without stashing changes that the agent is actively editing.
- If you launch a second agent in the same repository to fix an unrelated bug in your frontend, both agents start stepping on each other’s files, overwriting code, and corrupting lockfiles.
The common workaround is cloning the repository into multiple separate directories (portfolio-copy1, portfolio-copy2). That works, but it wastes disk space, duplicates gigabytes of .git history, and forces you to constantly fetch and rebase between clones.
There has been a better tool built directly into Git for over a decade: Git Worktrees.
When paired with modern AI coding workflows, git worktrees transform agentic development from a sequential waiting game into a parallel development pipeline.
What is a Git Worktree?
In a standard Git setup, one repository folder maps to one checked-out branch.
A git worktree allows you to check out multiple branches simultaneously into separate directories, all backed by the same underlying .git directory:
~/projects/portfolio/ (Main repo & .git store)
│
├── branch: main
│
├── Worktree 1: ~/projects/portfolio-worktrees/agent-auth
│ └── branch: fix/token-refresh-leak
│
└── Worktree 2: ~/projects/portfolio-worktrees/agent-ui
└── branch: feature/skills-filter-keyboard
Both worktrees share the exact same commit history, tags, and remotes. When an agent commits a change inside agent-auth, that commit is immediately visible in your main repository’s git log without needing to push or pull.
Setting up a dedicated worktree sandbox for agents
I keep worktrees sibling to the main repository in a dedicated directory. This prevents accidental nested commits and keeps project roots clean:
# 1. Create a worktrees directory alongside the project
mkdir -p ../portfolio-worktrees
# 2. Add a new worktree on a fresh branch for the AI agent
git worktree add ../portfolio-worktrees/agent-auth -b fix/token-refresh-leak
Within a fraction of a second, ../portfolio-worktrees/agent-auth exists with a complete, clean checkout of your codebase on the fix/token-refresh-leak branch.
Handling dependencies and environment files
A new worktree contains your tracked source files, but ignores uncommitted items (like .env files, node_modules/, or vendor/ directories).
You need two quick steps before pointing an AI agent at the worktree:
cd ../portfolio-worktrees/agent-auth
# Copy the local environment file
cp ../../portfolio/.env.example .env
# Install or link dependencies
pnpm install
Why independent dependency directories matter: Never symlink
node_modulesorvendorbetween active worktrees. If Agent A runscomposer requireorpnpm addwhile Agent B is running a test suite in another worktree, the lockfile and package directories will collide mid-execution. Disk space is cheap; isolation is what prevents corrupted test runs.
Launching the AI agent in its sandbox
Now you can point your AI agent directly at the isolated directory:
cd ../portfolio-worktrees/agent-auth
claude
Inside this isolated directory:
- The agent has full terminal access, can run tests, can edit any file, and can commit changes.
- Your primary IDE remains completely clean. You can continue writing code on
mainor an active sprint branch without interruption. - You can open a third terminal window and spawn another agent on a completely different task:
git worktree add ../portfolio-worktrees/agent-migrations -b refactor/sqlite-v3
cd ../portfolio-worktrees/agent-migrations
claude
You are no longer an engineer waiting for an AI to finish its run. You are an engineering lead overseeing two isolated branches being developed simultaneously.
A 10-line helper script for rapid agent spawning
To avoid typing the worktree boilerplate repeatedly, add this small bash helper to your ~/.zshrc or project scripts:
agent-tree() {
local name="$1"
local branch="agent/$name"
local target="../$(basename "$PWD")-trees/$name"
if [ -z "$name" ]; then
echo "Usage: agent-tree <task-slug>"
return 1
fi
git worktree add "$target" -b "$branch"
cp .env "$target/.env" 2>/dev/null || true
echo "Worktree created at: $target"
echo "Branch: $branch"
echo "Ready for agent launch: cd $target"
}
Now creating an isolated sandbox for an agent is a single command:
agent-tree fix-api-response
Reviewing and merging when the agent finishes
When the agent notifies you that its task is complete, reviewing its work takes advantage of standard Git commands from your primary workspace:
# From your main repository
cd ~/projects/portfolio
# 1. Compare the agent's branch directly
git diff main...agent/fix-api-response
# 2. Run the main project test suite against that branch
git checkout agent/fix-api-response
pnpm test
# 3. Fast-forward or rebase merge into your active branch
git checkout main
git merge agent/fix-api-response --ff-only
Once merged, cleaning up the worktree takes one command:
# Remove the directory and unregister the worktree
git worktree remove ../portfolio-worktrees/agent-auth
# Delete the temporary task branch if merged
git branch -d agent/token-refresh-leak
If you manually delete a directory without running git worktree remove, clean up stale references with:
git worktree prune
The mindset shift: supervisor over spectator
Developers who get frustrated with AI coding tools often treat them like synchronous search engines: they type a prompt, sit back, watch the tokens stream in, wait for tests to complete, and lose their train of thought.
Using Git worktrees changes that dynamic:
- Keep the human thread uninterrupted: Your main editor never leaves your primary task.
- Deterministic blast radius: If an agent hallucinates, runs a rogue regex, or breaks dozens of files, you don’t have to
git reset --hardyour working tree. You simply delete that worktree folder and your actual work is untouched. - True parallel throughput: Routine refactoring, test additions, and dependency upgrades can happen concurrently in the background while you focus on core product architecture.
Agentic coding tools are only as effective as the boundaries you put around them. Git worktrees provide the cleanest boundary available.