TLDR: Git worktrees let you have multiple branches checked out at the same time in separate directories, all sharing the same git repository. Perfect for running multiple coding agents in parallel without interference.
Normally, one git repo = one working directory = one branch at a time. Switching branches changes files for everyone. With worktrees, you can have multiple branches active simultaneously, each in its own folder.
See current worktrees:
git worktree list
Create worktree with a new branch:
git worktree add ../path-to-new-folder -b new-branch-name
Create worktree from existing branch:
git worktree add ../path-to-new-folder existing-branch-name
Remove a worktree:
git worktree remove /path/to/worktree
.git/ directory).git file that points back to the main repogit worktree add ../agent-feature-x -b feature-xcd into each worktreeclaude)git merge feature-xgit worktree remove ../agent-feature-x and git branch -d feature-xBelow is a dialog I had in the Solve It platform so I could walk through the process of learning about git worktrees.
I want to learn about git worktrees. I've never used them before. I am familiar with basic git commands such as
git checkout -b <branch name>
git add
git commit -m
git push
The reason I want to learn about git worktrees is because I want to run coding agents on the same project.
Scenarios I am interested in learning to do are:
Here is the documentation on git worktrees:
We will begin by cloning a demo repo: https://github.com/octocat/Hello-World.
I want to have a solid tutorial on the basics of git worktrees for my primary use case (running multiple agents in parallel on different branches). After cloning the repo we will use Python os to change the working directory to the root of the cloned repo.
We will simulate an agent making changes on the primary worktree and then have two other worktrees. This is why I remove these three folders below for a fresh slate each time this notebook is run.
!rm -rf Hello-World
!rm -rf agent-bugfix1
!rm -rf agent-bugfix2
I would like for you to walk me through a tutorial on git worktrees in this notebook. Ready to begin?
Great! I'm ready to help you learn git worktrees. This is a really useful feature for your use case of running multiple agents in parallel on different branches.
Let's start by setting up the demo repository. First, run those cleanup and clone commands:
!rm -rf Hello-World agent-bugfix1 agent-bugfix2
!git clone https://github.com/octocat/Hello-World
After that runs successfully, we'll change into the repository directory. Let me know once you've run this and we'll take the next step!
!rm -rf Hello-World agent-bugfix1 agent-bugfix2
!git clone https://github.com/octocat/Hello-World
import os
os.chdir('Hello-World')
# Verify we're in the right place
!pwd
!ls
!git status
!git branch
Let's start from the very beginning. Don't skip things I should know.
Perfect! Let's start with the fundamentals of git worktrees.
Normally, a git repository has one working directory where you check out branches. If you want to work on a different branch, you have to switch branches with git checkout, which changes all the files in your working directory.
A worktree lets you have multiple working directories for the same repository. Each worktree can have a different branch checked out. This means you can:
Right now, you're in what's called the main worktree (or primary worktree). This is the original Hello-World directory you cloned. Let's see what we have:
!git worktree list
Run this command. It will show you all worktrees associated with this repository. Right now, you should only see one - the main worktree on the master branch.
!git worktree list
So the first thing I notice is I have been using git worktrees all along (the primary branch), right? I don't think of it as its own "tree/folder". I just think of it as the root of the repo.
Exactly! That's a great observation. You've been using a worktree all along - you just didn't know it had that name!
The main worktree is indeed just the root of your repo. When git added the worktree feature, they didn't change how the main repository works at all. They just made it possible to create additional worktrees that link back to the same git repository.
So the mental model is:
git checkout to switch branches.git directoryThink of it like this: the main worktree is your "home base," and additional worktrees are like "branch offices" - they all share the same git history and configuration, but each can work on different branches independently.
Ready to create your first additional worktree?
Yep. Please cover the commands for creating a new branch as well as assuming I'm already on a branch. For example the Claude Code docs say
# Create a new worktree with a new branch
git worktree add ../project-feature-a -b feature-a
# Or create a worktree with an existing branch
git worktree add ../project-bugfix bugfix-123
Great! Let's break down both scenarios:
git worktree add ../agent-bugfix1 -b bugfix1
This does two things:
bugfix1 (the -b flag means "create branch")../agent-bugfix1 with that branch checked outThe new branch starts from wherever you currently are (in this case, master).
git worktree add ../agent-bugfix2 bugfix2
This assumes the branch bugfix2 already exists. It:
../agent-bugfix2bugfix2 branch thereNo -b flag needed since the branch already exists.
../agent-bugfix1) is where the new worktree folder will be created (relative to your current location)Ready to try creating your first worktree?
!git branch
!git checkout -b bugfix-1
!git worktree add ../agent-bugfix1 bugfix-1
I guess this is what you said above about one branch per worktree?
Exactly! You've just hit that restriction. Git is telling you: "Hey, bugfix-1 is already checked out in the main worktree (/app/data/Hello-World), so I can't check it out in another worktree too."
This is actually a safety feature - it prevents conflicts from having the same branch active in multiple places at once.
To fix this, you have two options:
Option 1: Switch your main worktree back to master, then create the worktree with bugfix-1:
!git checkout master
!git worktree add ../agent-bugfix1 bugfix-1
Option 2: Create the worktree with a new branch instead:
!git worktree add ../agent-bugfix1 -b bugfix-1-agent
Which approach makes more sense for your use case of running agents in parallel?
!git checkout master
!git branch
!git worktree add ../agent-bugfix1 bugfix-1
Like that.
Perfect! Now you have your first additional worktree. Let's see what you've got:
!git worktree list
Run this to see both worktrees - your main one on master and the new one on bugfix-1.
!git worktree list
pwd
ls
os.chdir('..')
pwd
ls
So I see the two worktrees beside eachother. They also just look like two ordinary folders. But they are the same git repo? That's a little confusing to me. Maybe just because I usally think of a git repo as a single folder. Can you explain what's happening here? I assume they are not just 2 different git clones. You said they shared the same .git
Great question! This is the key insight about worktrees.
You're right - they're not two separate clones. They share the same .git directory. Let's look:
!ls -la Hello-World/.git
!ls -la agent-bugfix1/.git
Run both of these. You'll see something interesting about the .git in the worktree folder - it's different from the main one.
!ls -la Hello-World/
!ls -la agent-bugfix1/
and?
Notice the size difference!
Hello-World/.git is a directory (4096 bytes)agent-bugfix1/.git is a file (59 bytes)Let's see what's inside that file:
!cat agent-bugfix1/.git
This will show you the secret - it's just a pointer back to the main .git directory!
!cat agent-bugfix1/.git
so?
There it is! The .git file in agent-bugfix1 is just a pointer that says "my real git data is at /app/data/Hello-World/.git/worktrees/agent-bugfix1".
So here's what's happening:
.gitThis means:
agent-bugfix1, that commit goes into the shared repositoryWant to see this in action by making a change in the worktree?
Yes, I think this will all become more clear after running some examples and inspecting.
I guess we should begin by using os.chdir
Exactly! Let's change into the worktree directory:
os.chdir('agent-bugfix1')
After you run that, check where you are and what branch you're on:
!pwd
!git branch
os.chdir('agent-bugfix1')
!pwd
!git branch
ok
Perfect! You're now in the agent-bugfix1 worktree on the bugfix-1 branch (the * shows which branch is active).
Let's make a change here to simulate what an agent might do. Create a new file:
!echo "Bug fix #1: Fixed critical issue" > bugfix1.txt
!git status
This will show you the new file as untracked. What do you think will happen if we look at git status back in the main worktree?
!echo "Bug fix #1: Fixed critical issue" > bugfix1.txt
!git status
!git add bugfix1.txt
! git commit -m "Bug Fix 1"
Now what
Great! You've committed a change on the bugfix-1 branch in the worktree. Now let's go back to the main worktree and see what it looks like there:
os.chdir('../Hello-World')
Then check:
!pwd
!git branch
!ls
What branch do you think you'll be on, and will you see the bugfix1.txt file?
os.chdir('../Hello-World')
!pwd
!git branch
!ls
Now it's not there. Because I'm on the master branch, right?
Exactly! You're on the master branch, and bugfix1.txt only exists on the bugfix-1 branch.
But notice something important - you can see bugfix-1 in the branch list (with the + symbol). That + means it's checked out in another worktree. The commit you made in the agent-bugfix1 worktree is now part of the shared repository.
Want to verify that commit exists? Try:
!git log bugfix-1 --oneline
This will show you the commit history of the bugfix-1 branch, including your new commit, even though you're currently on master.
!git log bugfix-1 --oneline
And I would get an error if I tried to checkout that branch on this worktree because the other worktree already has that branch checked out, correct?
Exactly right! Let's verify that:
!git checkout bugfix-1
Try it and see what error message git gives you.
!git checkout bugfix-1
Next I am going to check out a branch on the primary worktree and edit the README. Then I will create a bugfix-2 worktree and branch in one go.
!git branch
!git checkout -b update-readme
!echo "Updated by agent" >> README
!git status
!git add README
!git commit -m 'update the README'
!git status
!git branch
!git worktree list
!git checkout master
!git worktree add ../agent-bugfix2 -b bugfix-2
os.chdir('../agent-bugfix2/')
!git branch
ls
!echo "This is a second bugfix" > bugfix2.txt
!git status
!git add bugfix2.txt
!git commit -m 'fixing the second bug'
os.chdir('../Hello-World/')
!git checkout master
Okay so I think I have simulated "three agents" working on three different worktrees right?
Yes, exactly! You've simulated three agents working independently:
Hello-World/) - worked on update-readme branch, modified READMEbugfix-1 branch, created bugfix1.txtbugfix-2 branch, created bugfix2.txtAll three are completely independent - they didn't interfere with each other, and each has their own changes committed to their own branch.
Let's verify all three branches exist with their changes:
!git branch -v
This will show all branches with their latest commit messages. What do you want to explore next - maybe merging these branches, or cleaning up worktrees?
!git branch -v
Now lets merge these changes into master.
! git branch
!pwd
!git merge update-readme
!git merge bugfix-1 --no-edit
!git merge bugfix-2 --no-edit
!git status
!git log
Now what? Worktree cleanup?
Yes! Now let's clean up the worktrees. Once you've merged the changes, you typically don't need those separate worktrees anymore.
First, let's see what worktrees you have:
!git worktree list
Then to remove a worktree, you use:
git worktree remove <path>
Try removing one of them. Which one do you want to remove first?
!git worktree list
!git worktree remove /app/data/work_tree_demo/agent-bugfix1
!git worktree remove /app/data/work_tree_demo/agent-bugfix2
!git worktree list
And done?
Almost! The worktree directories are removed, but the branches still exist. Check:
!git branch
You'll see bugfix-1, bugfix-2, and update-readme are still there. Do you want to delete those branches too, or keep them?
!git branch
!git branch -D bugfix-1 bugfix-2 update-readme
Okay I think I have a good understanding of the basics now.
I would run my agent from within the git worktree.
So I would cd into each worktree directory and run claude to open claude coding agent etc.
I could have multiple terminal tabs opened, each with their own worktree.
The reason I could not do this before was because I was only ever using the primary worktree which only supports one branch at a time (checking out a branch would change it in all terminals).
Exactly! You've got it perfectly.
Before worktrees:
With worktrees:
cd into a different worktreeSo your workflow would be:
git worktree add ../agent-feature-x -b feature-xcd into each worktreeclaude) in each terminalThe key benefit: no interference - each agent has its own isolated workspace with its own branch checked out.
Is there anything else about worktrees you'd like to explore before wrapping up this tutorial?
Will files that are not committed to git on the primary worktree get copied to the other worktrees? Let's check that. One other thing I was curious about.
!echo "This is not committed" > temp_file.txt
!git status
!git worktree list
!git branch
!git worktree add ../new-feature -b new-feature
os.chdir('../new-feature/')
!git branch
!git status
!ls
Not copied.
Correct! Untracked/uncommitted files are not copied to new worktrees.
Each worktree only contains:
So in your case:
temp_file.txt exists only in the main worktree (untracked)new-feature worktree doesn't have itnew-feature worktree has README, bugfix1.txt, and bugfix2.txt because those were committed to master (which new-feature branched from)This is actually perfect for your agent use case - each agent's workspace is clean and only contains committed code. Any temporary files, logs, or work-in-progress that an agent creates stays isolated in its own worktree.
Does this complete your understanding of worktrees, or is there anything else you'd like to test?
!git worktree list
!git worktree remove /app/data/work_tree_demo/new-feature