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 Home | Git Worktree |
|---|---|
| Original home stays untouched | Original code (main branch) stays intact |
| Build a model home next door | Create a new working folder alongside |
| Freely experiment with interior design | Freely modify/experiment with code |
| Like it? Apply to real home | Like it? Merge (combine) |
| Don't like it? Demolish | Don't like it? Delete the folder |
| No impact on real home | No 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
| Item | Branch Switching (checkout) | Worktree |
|---|---|---|
| Workflow | Switch branches in same folder | Separate folder with independent workspace |
| Simultaneous work | Only one at a time | Multiple simultaneously |
| Original safety | Files change on switch | Original folder never changes |
| Cleanup | Delete branch (git branch -d) | Delete folder + git worktree remove |
| Complexity | Needs checkout, stash, etc. | Open/close folders level |
| Best for | Quick fixes | Experiments, 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)
/worktreeClaude 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 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# Move to worktree folder
cd ../my-experiment
# Work freely here!
# Completely independent from the original folder.Full Workflow — 4 Steps
/worktree or git worktree add to create a new workspace
Modify and run code in the worktree folder
Like it? Merge. Don't like it? Delete
git worktree remove to cleanly remove the worktree
Management Commands — All You Need
| Command | Description |
|---|---|
git worktree list | View current worktree list |
git worktree add ../folder branch-name | Create new worktree |
git worktree remove ../folder | Remove worktree |
git worktree prune | Clean up worktrees from deleted folders |
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
# 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-featureWhen You Don’t Like It — 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
"Build 3 pages" → main agent splits the work
Each team member agent gets an independent worktree automatically
Each works in their own worktree. No conflict worries
When all work is done, main agent combines everything into one
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.
| Task | Sequential | Agent 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.
# Run in your existing project folder
git worktree add ../my-test-experiment test/experiment
# Verify
git worktree list# Navigate to worktree folder
cd ../my-test-experiment
# Modify any file
echo "Experimenting!" > test-file.txt
# Commit
git add .
git commit -m "Worktree experiment"# 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.
# 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.
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.
# 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 branch24. 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.
git worktree add ../my-experiment feat/experiment
cd ../my-experiment
npm install # Do this first!
npm run dev5. 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.
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.
| Situation | Solution |
|---|---|
| Error when creating worktree | Run git worktree prune then retry |
| node_modules is missing | Run npm install in worktree folder |
| Merge conflict | Tell Claude Code "resolve the conflict" |
| Too many worktrees | Check with git worktree list and delete unused ones |
| Error: not a git repository | Need 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
- Git Worktree Official Docs — git-scm.com/docs/git-worktree
- Claude Code Agent Teams — docs.anthropic.com/en/docs/claude-code
- Git Branching Strategies — git-scm.com/book/en/v2/Git-Branching