We need to talk about your workflow. If you are using Claude Code or any agentic coding tool, you’ve likely hit the “working directory” bottleneck. You kick off a refactor, wait for the stream, and then realize that if you touch a single file, you’ll trigger a race condition between your brain and the model. Git Worktrees are the pragmatic fix for this mess, but they come with a setup tax that most blog posts ignore.
One Directory, One Train of Thought
A standard working directory is a physical constraint. It can only hold one state of the project at a time. If your AI Agent is halfway through a complex refactor on feature/checkout-api and you decide to sneak in a quick bug fix on main, you are asking for trouble. Switching branches mutates the disk. Either Git blocks you because of dirty files, or the agent gets confused when the underlying file system shifts under its feet. I’ve seen agents calmly revert human commits because they didn’t match the context from the start of the session. It’s not a “hallucination”; it’s a file system collision.
The solution isn’t “better prompting.” It’s giving the agent its own desk. That is exactly what Git Worktrees provide: a second working directory pointing at the same underlying repository database, locked to a different branch.
How Git Worktrees Solve Parallelism
Instead of cloning the whole repo again—which is a waste of disk space and a headache for remotes—you use the git worktree command. It creates a new folder, checks out a branch, and shares the .git history with your main directory. Changes in one are visible in the git log of the other instantly, but the files on disk remain isolated.
# Create a dedicated desk for your AI agent
git worktree add .worktrees/agent-refactor -b feature/refactor-logic
Now you can have your editor open in your main folder while the agent works in .worktrees/agent-refactor. No stepping on toes. If you’re using Claude Code, they recently added a first-class --worktree (or -w) flag that handles the directory creation and session launch in one step. For more on managing your code properly, check out my guide on WordPress GitHub Repositories.
The Setup Tax Nobody Warns You About
Here is the gotcha: A new worktree is a clean slate. Everything in your .gitignore is missing. In a modern WordPress or WooCommerce project, that means:
- Missing node_modules/ or .venv/: You have to reinstall every time.
- Empty .env files: Your app won’t boot without secrets.
- Port Collisions: Two worktrees both trying to bind to
:3000will fight.
If it takes five minutes to bootstrap a worktree for a ten-minute task, you’ll stop using them. This is where automation becomes mandatory. You need a script that doesn’t just run git worktree add, but also copies environment files and derives a unique port offset based on a hash of the directory path.
Automating the Agent Desk
I usually wrap this logic in a bash script (wt-setup.sh) and then expose it as a custom skill to the AI. This is consistent with how I approach AI in WooCommerce workflows—boring, deterministic scripts for the mechanical setup, and LLM reasoning for the branching and logic.
#!/bin/bash
# A senior dev's lazy way to bootstrap a worktree
BRANCH_NAME=$1
DIR=\".worktrees/$BRANCH_NAME\"
git worktree add $DIR -b $BRANCH_NAME
cp .env $DIR/.env
cd $DIR
npm install # Or wp-env start
echo \"Worktree ready at $DIR\"
By letting the agent “pay” the setup tax, you move from being a coder to an orchestrator. You decompose tasks into sub-branches, spin up three worktrees, and review the incoming PRs. This is the only way to scale agentic coding without losing your mind to merge conflicts.
Look, if this Git Worktrees stuff is eating up your dev hours, let me handle it. I’ve been wrestling with WordPress since the 4.x days.
The Takeaway
You and your AI agent cannot share a desk. Use Git Worktrees to parallelize your branches, not just your thoughts. For the technical details, refer to the official Git documentation or check out the Claude Code CLI reference. Stop switching branches and start opening windows.
\”},excerpt:{raw: