Git Worktree: One Repository, Multiple Working Directories
Have you ever been halfway through implementing a feature when an urgent bug fix comes in?
You stop what you’re doing, stash your changes, switch branches, fix the bug, switch back, and restore your changes.
Now imagine doing this several times a day while also running multiple AI coding agents.
There is a better way: Git worktree.
It’s one of the most useful yet often overlooked features of Git.
The Problem With Traditional Branch Switching
Consider a typical React application with three branches:
mainfeature/authfeature/dashboard
Normally, we switch between branches using:
git switch feature/authWhen we switch branches, Git updates the files in our working directory to match the selected branch.
main ───────────────┐
│
feature/auth ───────┼──> expense-app/
│
feature/dashboard ──┘We have multiple branches but only one working directory.
If we have unfinished changes, switching branches may require committing or stashing them first.
This becomes inconvenient when working on multiple features, reviewing pull requests, or handling urgent production issues.
What Is Git Worktree?
Git worktree allows us to check out multiple branches of the same repository into separate working directories simultaneously.
Instead of switching branches within one directory, we create additional directories connected to the same Git repository.
Git Repository
Shared .git data
|
+---------+---------+
| | |
v v v
expense-app expense-auth expense-dashboard
main feature/auth feature/dashboardEach working directory has its own:
- Checked-out source files.
- Current branch.
- Staging area.
- Uncommitted changes.
However, they share the underlying Git repository, commit history, and object database.
One repository. Multiple working directories.
Creating Your First Git Worktree
Let’s assume we have an existing repository called expense-app.
cd ~/dev/expense-appCheck the current branch:
git branchOutput:
* mainCreate a new worktree for authentication:
git worktree add -b feature/auth ../expense-auth mainLet’s break down the command.
| Argument | Purpose |
|---|---|
git worktree add |
Creates a linked working directory |
-b feature/auth |
Creates a new branch |
../expense-auth |
Specifies the new directory |
main |
Specifies the starting branch |
Now create another worktree for dashboard development:
git worktree add -b feature/dashboard ../expense-dashboard mainOur directory structure now looks like this:
dev/
├── expense-app/
│ ├── .git/
│ ├── src/
│ └── package.json
│
├── expense-auth/
│ ├── .git
│ ├── src/
│ └── package.json
│
└── expense-dashboard/
├── .git
├── src/
└── package.jsonWe can open all three directories in separate IDE windows and work on them independently.
No branch switching. No unnecessary stashing.
How Git Worktree Works Internally
Understanding the internals makes this feature even more interesting.
A traditional Git repository contains a .git directory that stores repository metadata.
expense-app/.git/
├── HEAD
├── config
├── index
├── objects/
├── refs/
└── worktrees/
├── expense-auth/
│ ├── HEAD
│ ├── index
│ ├── gitdir
│ └── commondir
│
└── expense-dashboard/
├── HEAD
├── index
├── gitdir
└── commondirNotice that the linked worktrees have their own metadata directories inside the main repository.
However, inside expense-auth, .git is not a directory. It is a small text file.
cd ../expense-auth
cat .gitExample output:
gitdir: /Users/developer/dev/expense-app/.git/worktrees/expense-authThis file tells Git where to find the metadata associated with that particular worktree.
What Is Shared?
- Git objects.
- Commit history.
- Local branch references.
- Remote-tracking references.
- Tags.
- Repository configuration by default.
- Stash entries.
What Is Independent?
- HEAD.
- Index or staging area.
- Checked-out source files.
- Uncommitted changes.
- Untracked files.
- Certain worktree-specific references.
The architecture can be visualized as:
Worktree A Worktree B
feature/auth feature/dashboard
| |
v v
Own HEAD and index Own HEAD and index
| |
+-------------+---------------+
|
v
Shared Git Data
Objects, refs, configWe can inspect these locations using Git itself.
git rev-parse --git-dir
git rev-parse --git-common-dirThe first command identifies the worktree-specific Git directory.
The second identifies the shared repository directory.
Git Worktree vs. Git Clone
Why not simply clone the repository multiple times?
That works, but worktrees provide a more efficient alternative for many local development workflows.
| Feature | Git Clone | Git Worktree |
|---|---|---|
| Working directory | Independent | Independent |
| Git object database | Normally separate | Shared |
| Commit history | Separate copy | Shared |
| Branch references | Independent | Shared |
| Uncommitted changes | Independent | Independent |
| Remote configuration | Independent | Normally shared |
| Creation speed | Generally slower | Generally faster |
| Disk usage | Generally higher | Generally lower |
| Parallel development | Supported | Supported |
With worktrees, we avoid maintaining multiple independent repositories for the same project.
However, source files are still checked out separately, so worktrees are not entirely free in terms of disk usage.
Practical Use Case: An Urgent Bug Fix
Imagine you are implementing a new authentication feature.
git switch feature/authYou have several unfinished changes when an urgent bug is reported.
Instead of stashing your changes, create another worktree:
git worktree add -b fix/login-validation ../expense-hotfix mainOpen the hotfix directory:
cd ../expense-hotfixImplement the fix, test it, and commit it.
git add .
git commit -m "Fix login validation"
git push -u origin fix/login-validationCreate a pull request as usual.
Meanwhile, your authentication work remains untouched in the original directory.
This is particularly useful when working on long-running features.
Using Git Worktree With Claude Code
AI coding agents make Git worktree even more valuable.
Running multiple agents inside the same working directory can create problems:
- Agents may overwrite each other’s changes.
- Unrelated modifications may become mixed together.
- Branch switching can interrupt another session.
- Reviewing individual changes becomes difficult.
Worktrees provide separate working directories for independent tasks.
Terminal 1: Authentication
cd ~/dev/expense-auth
claudeTask:
Implement authentication using the existing
application architecture.
Add appropriate tests and document the changes.Terminal 2: Dashboard
cd ~/dev/expense-dashboard
claudeTask:
Implement the dashboard components.
Follow the existing UI standards and reuse
shared components where possible.Terminal 3: Code Review
Create a dedicated worktree for reviewing the current main branch.
cd ~/dev/expense-app
git worktree add --detach ../expense-review main
cd ../expense-review
claudeTask:
Review the existing architecture.
Identify maintainability, performance,
and testing concerns.
Do not modify any files.The resulting workflow looks like this:
Shared Git Repository
|
+---------------+---------------+
| | |
v v v
Authentication Dashboard Code Review
Worktree Worktree Worktree
| | |
v v v
Claude Agent 1 Claude Agent 2 Claude Agent 3Each agent has its own working directory.
This makes it possible to develop multiple features and review code in parallel.
However, worktrees do not eliminate merge conflicts. If two agents modify the same lines of code, those changes may still conflict when merged.
Running Multiple Applications Simultaneously
Git worktrees isolate source files, but they do not automatically isolate the application runtime.
For a React and Node.js application, each worktree may require its own dependencies.
cd ~/dev/expense-auth
npm cicd ~/dev/expense-dashboard
npm ciYou may also need different application ports.
For a Vite application:
npm run dev -- --port 5173In another worktree:
npm run dev -- --port 5174For backend applications, configure separate ports and appropriate development environments.
| Worktree | Frontend | Backend |
|---|---|---|
| Main | 5173 | 3000 |
| Authentication | 5174 | 3001 |
| Dashboard | 5175 | 3002 |
Other resources to consider include:
- Environment variables.
- Database instances or schemas.
- Docker containers.
- Redis instances.
- Build artifacts.
- Local configuration files.
Untracked and ignored files, including .env files and node_modules, are not automatically copied into newly created worktrees.
Use separate development databases when changes could interfere with one another, and avoid exposing production credentials to coding agents.
Working With Existing Branches
You do not always need to create a new branch.
If a branch already exists locally, you can create a worktree for it.
git worktree add ../expense-auth feature/authTo review a remote branch:
git fetch origin
git worktree add --track -b review/payment \
../expense-payment-review origin/feature/paymentAlternatively, use a detached HEAD for temporary inspection.
git worktree add --detach ../expense-review origin/mainThis is useful for reviewing a specific commit without creating another branch.
Managing Worktrees
List All Worktrees
git worktree listExample output:
/dev/expense-app a1b2c3d [main]
/dev/expense-auth b2c3d4e [feature/auth]
/dev/expense-dashboard c3d4e5f [feature/dashboard]Move a Worktree
git worktree move ../expense-auth ../auth-serviceRemove a Worktree
Once development is complete and the changes have been committed and merged:
git worktree remove ../expense-authGit normally refuses to remove a worktree containing uncommitted or untracked changes.
This protects unfinished work.
Delete the Branch
Removing a worktree does not delete its branch.
After confirming that the branch is no longer needed:
git branch -d feature/authFor squash-merged branches, Git may not recognize the branch as fully merged. Verify that the changes are safely integrated before considering forced branch deletion.
Clean Up Stale Metadata
If a worktree directory was manually deleted, its metadata may remain.
git worktree prune --dry-runReview the output before cleaning up.
git worktree pruneImportant Git Worktree Restrictions
Git normally prevents the same branch from being checked out in multiple worktrees simultaneously.
For example, if main is already checked out in the original repository:
git worktree add ../another-app mainGit will reject the operation.
This prevents multiple working directories from independently updating the same branch.
If you only need to inspect the same commit, use a detached worktree:
git worktree add --detach ../another-app mainAlso remember that branch references are shared. A commit created in one worktree becomes available to the other worktrees through the shared Git repository.
The working files in those other directories do not automatically change.
Common Misconceptions
| Misconception | Reality |
|---|---|
| Worktrees are separate Git repositories | They share repository data |
| Uncommitted changes are shared | Each working directory is independent |
| Worktrees eliminate merge conflicts | Conflicts can still occur during merging |
| Removing a worktree deletes its branch | Branch deletion is a separate operation |
| Worktrees automatically share dependencies | Dependencies must be managed separately |
| Worktrees are security sandboxes | They provide working-directory separation, not security isolation |
Git Worktree Command Cheat Sheet
| Command | Description |
|---|---|
git worktree list |
List registered worktrees |
git worktree add -b branch path main |
Create a worktree and branch |
git worktree add path branch |
Use an existing branch |
git worktree add --detach path HEAD |
Create a detached worktree |
git worktree remove path |
Remove a linked worktree |
git worktree move old new |
Move a linked worktree |
git worktree prune |
Remove stale metadata |
git worktree lock path |
Protect a worktree from pruning |
git worktree unlock path |
Unlock a worktree |
git worktree repair |
Repair worktree administrative links |
Recommended Development Workflow
Start from an updated main branch
|
v
Create Git worktree
|
v
Implement the feature
|
v
Run local tests
|
v
Review and commit code
|
v
Push feature branch
|
v
Create pull request
|
v
Merge changes
|
v
Remove worktree
|
v
Delete obsolete branchWhen Should You Use Git Worktree?
Git worktree is particularly useful for:
- Developing multiple features simultaneously.
- Handling urgent bug fixes.
- Reviewing pull requests.
- Testing different versions of an application.
- Running parallel AI coding agents.
- Performing long-running refactoring.
- Comparing implementations across branches.
- Maintaining multiple release branches.
For simple projects where you only work on one task at a time, traditional branch switching may still be sufficient.
Final Thoughts
Git worktree changes how we think about branches.
Instead of treating a repository as a single directory that constantly switches between branches, we can treat it as a shared Git database with multiple independent working environments.
This becomes especially valuable as software development moves toward parallel AI-assisted workflows.
We do not need multiple clones, constant branch switching, or unnecessary stashing just to work on independent tasks.
Sometimes, the most useful productivity improvements are already available in the tools we use every day.
One repository. Multiple branches. Multiple working directories.
That’s the power of Git worktree.