AI for Automation
🌿 Git Worktree — Safe Experimentation

Step 23 / 22

🌿 Git Worktree — Safe Experimentation

Experiment without touching the original code

Git Worktree = A Safe Space to Experiment

Ever been editing code and thought “I messed something up”? You try to undo, but can’t remember how far back the changes go. Worried you might break the original. We’ve all been there.

Git Worktree solves this cleanly. Keep your original code untouched, create a completely independent copy alongside it, and experiment there freely.

Think of it like a model home. Your actual home (the original) stays untouched, while you build a model home (worktree) right next door to try new interior designs, wallpaper, furniture arrangements. Like it? Apply it to your real home. Don’t like it? Demolish the model home. Done.

Model HomeGit Worktree
Original home stays untouchedOriginal code (main branch) stays intact
Build a model home next doorCreate a new working folder alongside
Freely experiment with interior designFreely modify/experiment with code
Like it? Apply to real homeLike it? Merge (combine)
Don't like it? DemolishDon't like it? Delete the folder
No impact on real homeNo impact on original branch

One-line summary

Git Worktree = A separate space to experiment safely without touching the original. Experiment succeeds? Merge. Fails? Delete.

Why Do You Need Worktree?

“Can’t I just create a branch?” Yes, branches work for experiments too. But Worktree is much more convenient in several ways.

📂

Separate Folders

Regular branches switch within the same folder. Worktrees create a completely separate folder. You can have original and experiment folders open simultaneously.

Original Always Safe

Even if something goes wrong during experiments, the original folder is completely unaffected. A completely different level of psychological safety. “Let’s just try it” becomes possible.

🚀

Simultaneous Work Without Switching

Branches require switching one at a time. Worktrees let you open multiple simultaneously. Build feature A while testing feature B at the same time.

🗑

Clean Cleanup

When the experiment is done, just delete the folder. No complicated git commands. Simple as creating and deleting a temp folder.

Branch Switching vs Worktree

ItemBranch Switching (checkout)Worktree
WorkflowSwitch branches in same folderSeparate folder with independent workspace
Simultaneous workOnly one at a timeMultiple simultaneously
Original safetyFiles change on switchOriginal folder never changes
CleanupDelete branch (git branch -d)Delete folder + git worktree remove
ComplexityNeeds checkout, stash, etc.Open/close folders level
Best forQuick fixesExperiments, large features, team work

When to use Worktree?

• Want to experiment with a new feature
• Making big changes and worried about breaking the original
• Need to develop multiple features simultaneously
• Using with Claude Code’s Agent Teams (explained below)

Worktree = Model Home

Original home (code) stays intact — experiment freely next door

Creating a Worktree — One Line with /worktree

In Claude Code, creating a Worktree is incredibly easy. One slash command.

Method 1: Claude Code Slash Command (Easiest)

Enter in Claude Code
/worktree

Claude Code automatically creates a new branch, generates the worktree folder, and navigates to it. One line, done.

Method 2: Direct Git Commands

If you prefer doing it manually without the slash command:

Create worktree (with new branch)
# Create new worktree in parent folder
git worktree add ../my-experiment feat/new-feature

# Explanation:
# ../my-experiment  → folder path for the worktree
# feat/new-feature  → new branch name
Navigate and work in worktree
# Move to worktree folder
cd ../my-experiment

# Work freely here!
# Completely independent from the original folder.

Full Workflow — 4 Steps

1
Create worktree

/worktree or git worktree add to create a new workspace

2
Experiment freely

Modify and run code in the worktree folder

3
Review results and decide

Like it? Merge. Don't like it? Delete

4
Clean up

git worktree remove to cleanly remove the worktree

Management Commands — All You Need

CommandDescription
git worktree listView current worktree list
git worktree add ../folder branch-nameCreate new worktree
git worktree remove ../folderRemove worktree
git worktree pruneClean up worktrees from deleted folders
Check worktree list
git worktree list

# Example output:
# /home/user/my-project       abc1234 [main]
# /home/user/my-experiment    def5678 [feat/new-feature]

When You Like the Result — Merge

Merge experiment results into original
# 1. Commit changes in worktree
cd ../my-experiment
git add .
git commit -m "New feature complete"

# 2. Go back to original folder and merge
cd ../my-project
git merge feat/new-feature

# 3. Clean up worktree
git worktree remove ../my-experiment
git branch -d feat/new-feature

When You Don’t Like It — Delete

Experiment failed! Clean delete
# Delete worktree (folder also deleted)
git worktree remove ../my-experiment

# Delete branch too (optional)
git branch -D feat/new-feature

# Done! Original is completely unchanged.

Claude Code handles it all

No need to memorize the git commands above. Just tell Claude Code “create a worktree,” “merge the experiment results,” or “delete the worktree” in plain English.

Combining with Agent Teams

Worktree’s real power shows when combined with Agent Teams. Agent Teams let Claude Code create multiple “team members (agents)” that work simultaneously.

The problem is that multiple agents working in the same folder simultaneously can overwrite files or cause conflicts. Like 3 chefs using one cutting board at the same time. Dangerous.

So Agent Teams give each agent their own worktree. Each chef gets their own cutting board. Work independently, then combine results when done.

Example: Build 3 Website Pages Simultaneously

Without Worktree

Build page A → finish → build page B → finish → build page C. Sequential = 3x the time.

Worktree + Agent Teams

Agent A (worktree A), Agent B (worktree B), Agent C (worktree C) work simultaneously. 3x faster!

How Agent Teams + Worktree Works

1
Main agent distributes tasks

"Build 3 pages" → main agent splits the work

2
Worktree created per member

Each team member agent gets an independent worktree automatically

3
Simultaneous independent work

Each works in their own worktree. No conflict worries

4
Merge results

When all work is done, main agent combines everything into one

How to use with Agent Teams
Build 3 pages for our project simultaneously.

1. Homepage - hero section + intro + CTA
2. Services page - 3 service cards layout
3. Team page - team member profile grid

Work independently and merge when done.

Give this instruction and Claude Code auto-creates 3 worktrees, 3 agents work simultaneously, then merges results. You just review the final output.

TaskSequentialAgent Teams + Worktree
3 pages~30 min (10 min x 3)~12 min (parallel)
5 bug fixes~25 min (5 min x 5)~7 min (parallel)
Test code writing~40 min (sequential)~15 min (parallel)

Agent Teams is Max subscription only

Agent Teams requires Claude Max subscription ($100/mo). Pro ($20/mo) allows only 1 agent. However, Worktree itself works fine on Pro! Just use git worktree commands manually.

Worktree + Agent Teams = Parallel Work

Each agent works in an independent space simultaneously, then merge results

Try It Yourself

Create a Worktree yourself. If you have git installed and a git-managed project, you can start right away.

Exercise 1: Create and Experiment with a Worktree

Let’s create a simple experimental worktree.

Step 1: Create worktree in project folder
# Run in your existing project folder
git worktree add ../my-test-experiment test/experiment

# Verify
git worktree list
Step 2: Move to worktree and experiment
# Navigate to worktree folder
cd ../my-test-experiment

# Modify any file
echo "Experimenting!" > test-file.txt

# Commit
git add .
git commit -m "Worktree experiment"
Step 3: Verify original folder
# Go back to original folder
cd ../my-project

# test-file.txt is not here! Original is safe.
ls test-file.txt
# File not found!

Exercise 2: Use Worktree via Claude Code

Much simpler through Claude Code.

Enter in Claude Code
# Method 1: Slash command
/worktree

# Method 2: Plain English
Create a worktree to experiment with a new feature.
I want to try changing the homepage design to a blue theme.
Be bold since we can just delete if I don't like it.

Exercise 3: Merge Experiment Results

If you like the experiment, have Claude Code merge it too.

Enter in Claude Code
I like the blue theme from the worktree.
Merge it into the original project.
And clean up the worktree.

Verification Checklist

If the exercise doesn’t work, make sure it’s a git repository. Git worktree doesn’t work in regular folders. Run git init to initialize a git repo first, or try in a project already managed by git.

Tips + Precautions

Useful tips and precautions for real-world Worktree usage.

1. Only works in git repositories

Worktree only works in git repositories. Regular folders without git won’t work. If you’re not using git yet, run git init to initialize or ask Claude Code to “set up git for this project.”

2. Check disk space

Worktrees create copies of the project, so they use disk space. Git manages it smartly (uses much less than a full copy), but creating 10 worktrees will add up. Delete finished worktrees immediately.

3. Establish naming conventions

Multiple worktrees can get confusing. Include the purpose in folder names for easy management.

Naming convention examples
# Good names (clear purpose)
git worktree add ../mk-login-page feat/login
git worktree add ../mk-bug-fix-123 fix/issue-123
git worktree add ../mk-redesign feat/redesign

# Bad names (unclear)
git worktree add ../test1 branch1
git worktree add ../temp branch2

4. For npm projects, don’t forget install

When you create a worktree for a Node.js project, the node_modules folder won’t exist. Run npm install first in the worktree folder.

Starting npm project worktree
git worktree add ../my-experiment feat/experiment
cd ../my-experiment
npm install   # Do this first!
npm run dev

5. Use with Pull Requests

Push worktree work as a GitHub PR to get team review before merging. This is the most common real-world pattern.

Tell Claude Code
Push the feature from the worktree as a PR.
I want to get a review before merging.

6. Can’t open the same branch in 2 places

One branch can only be open in one worktree at a time. If main is used in the main folder, the worktree needs a different branch name. Git handles this automatically, so don’t worry.

SituationSolution
Error when creating worktreeRun git worktree prune then retry
node_modules is missingRun npm install in worktree folder
Merge conflictTell Claude Code "resolve the conflict"
Too many worktreesCheck with git worktree list and delete unused ones
Error: not a git repositoryNeed to initialize with git init

Worktree = A Space Where Failure Is OK

The original is always safe, so “let’s just try it” becomes possible. When experimentation becomes routine, growth accelerates.

Don't be afraid to experiment

The most important thing in coding is “just trying it.” With Worktree, there’s no fear of breaking the original. You can be bolder. Just tell Claude Code “create a worktree and experiment with things” — one sentence to get started!

References