git worktree is a built-in Git feature that attaches multiple working directories to a single repository. Each worktree checks out its own branch or commit into a separate folder with its own HEAD and staging index, while sharing the underlying .git object store and branch references.
When developing with coding agents in an Agent IDE, worktrees often fit better than switching branches inside one checkout. Standard branches share a single set of files on disk: switching branches while an agent edits code or runs tests breaks running processes, invalidates file watchers, or forces frequent git stash detours. Giving each task its own worktree keeps edits separate without duplicating repository history. Tools covered earlier on this site—including Orca, T3 Code, and Herdr—all rely on worktrees for parallel workspaces.
Where Single-Directory Branch Switching Breaks Down
In a regular checkout, one repository maps to one directory on disk. Once multiple coding agents run concurrently, or you edit alongside an agent, a single working directory hits collisions at two points in time:
sequenceDiagram
autonumber
participant Dev as Developer / Editor
participant Dir as Single Working Directory
participant Agent as Background Agent
rect rgb(255, 241, 214)
Note over Dev,Agent: Case 1: Parallel edits in one directory
Dev->>Dir: Edit login.ts (uncommitted)
Agent->>Dir: Refactor api.ts and run tests
Note over Dir: Overlapping edits and builds, tests run on partial files
end
rect rgb(254, 226, 226)
Note over Dev,Agent: Case 2: Mid-task branch switch
Dev->>Dir: Run git switch hotfix
Dir--xDev: Blocked by dirty files, forced to git stash
Dir--xAgent: Underlying files shift, tests and session break
end
Creating separate git clone folders avoids directory overlap, but duplicates the entire .git history on disk and leaves local commits isolated until pushed to a remote and fetched back.
How Git Worktrees Share History and Isolate Files
git worktree separates repository storage from checkout state.
The original directory created by git init or git clone is the main worktree, which contains the .git/ folder holding commit objects (.git/objects), branch and tag references (.git/refs), and remote settings. Each extra directory created with git worktree add is a linked worktree. Instead of a full .git/ directory, it contains a .git text file pointing back to a dedicated metadata folder under .git/worktrees/<id>/ in the main repository.
block-beta
columns 3
block:shared:3
columns 3
st["Main .git/ Store (Shared)"]:3
obj["objects (commits)"]
refs["refs (branches & tags)"]
cfg["config (remotes)"]
end
space:3
block:mainwt:1
columns 1
m0["Main Worktree repo/"]
m1["Working files"]
m2["HEAD: main"]
m3["Own index"]
end
block:wt1:1
columns 1
w10["Linked repo-login/"]
w11[".git points to worktrees/"]
w12["HEAD: feat/login"]
w13["Own index"]
end
block:wt2:1
columns 1
w20["Linked repo-ui/"]
w21[".git points to worktrees/"]
w22["HEAD: feat/ui"]
w23["Own index"]
end
mainwt --> shared
wt1 --> shared
wt2 --> shared
style shared fill:#e7f0ff,stroke:#2563eb,color:#132b55
style st fill:#dbeafe,stroke:#2563eb,color:#132b55
style obj fill:#ffffff,stroke:#93c5fd,color:#132b55
style refs fill:#ffffff,stroke:#93c5fd,color:#132b55
style cfg fill:#ffffff,stroke:#93c5fd,color:#132b55
style mainwt fill:#eef4f8,stroke:#58708a,color:#20374f
style m0 fill:#dbeafe,stroke:#58708a,color:#20374f
style m1 fill:#ffffff,stroke:#cbd5e1,color:#20374f
style m2 fill:#ffffff,stroke:#cbd5e1,color:#20374f
style m3 fill:#ffffff,stroke:#cbd5e1,color:#20374f
style wt1 fill:#e8f7ef,stroke:#15803d,color:#14532d
style w10 fill:#dcfce7,stroke:#15803d,color:#14532d
style w11 fill:#ffffff,stroke:#86efac,color:#14532d
style w12 fill:#ffffff,stroke:#86efac,color:#14532d
style w13 fill:#ffffff,stroke:#86efac,color:#14532d
style wt2 fill:#e8f7ef,stroke:#15803d,color:#14532d
style w20 fill:#dcfce7,stroke:#15803d,color:#14532d
style w21 fill:#ffffff,stroke:#86efac,color:#14532d
style w22 fill:#ffffff,stroke:#86efac,color:#14532d
style w23 fill:#ffffff,stroke:#86efac,color:#14532d
Two properties follow directly from this layout:
- Commits and branches stay synchronized: Running
git commitinsiderepo-loginmakes that commit immediately visible togit log,git merge, orgit cherry-pickin the main checkout without pushing to a remote. - One branch cannot be checked out in two worktrees at once: Git blocks checking out an already active branch in a second worktree so two directories cannot advance the same branch pointer independently. For read-only inspection or temporary testing on an existing branch, pass
--detach.
Choosing Between Branches, Worktrees, and Full Clones
| Dimension | Single-Checkout Branch | Git Worktree | Separate git clone |
|---|---|---|---|
| Working directories | 1 | Multiple (per task) | Multiple |
.git history storage |
1 copy | Shared 1 copy | 1 copy per clone |
| Cross-task local commit visibility | Same directory | Immediate without push | Requires remote or local remote fetch |
| Parallel agent safety | Prone to file collisions | Isolated files and index | Isolated files and repo config |
Need git stash to switch tasks |
Usually yes | No | No |
Ignored files (.env, node_modules) |
Present in main checkout | Absent until copied or installed | Absent until copied or installed |
Plotting common workflows by setup cost and parallel isolation shows where each strategy lands:
---
config:
quadrantChart:
chartWidth: 680
chartHeight: 400
themeVariables:
quadrant1Fill: "#f3e8ff"
quadrant2Fill: "#e8f7ef"
quadrant3Fill: "#eef4f8"
quadrant4Fill: "#fff1d6"
quadrant1TextFill: "#4c1d95"
quadrant2TextFill: "#14532d"
quadrant3TextFill: "#20374f"
quadrant4TextFill: "#713508"
quadrantPointFill: "#2563eb"
quadrantPointTextFill: "#1e293b"
quadrantTitleFill: "#1e293b"
---
quadrantChart
title Isolation Strategy by Workflow
x-axis Low Setup & Disk Cost --> High Setup & Disk Cost
y-axis Low Parallel Isolation --> High Parallel Isolation
quadrant-1 Separate git clone
quadrant-2 Git Worktree
quadrant-3 Main Checkout Branch
quadrant-4 High Cost / Low Isolation
Quick single fix: [0.16, 0.22]
Review PR with dirty tree: [0.22, 0.78]
Parallel AI agents: [0.34, 0.90]
Side-by-side test runs: [0.35, 0.66]
Isolated config or repo experiments: [0.80, 0.86]
Essential Commands and Lifecycle
A linked worktree moves through the following states from creation to cleanup:
stateDiagram-v2
direction LR
[*] --> Active : add
Active --> Locked : lock
Locked --> Active : unlock
Active --> Stale : Manual rm -rf or mv
Stale --> Active : repair
Stale --> [*] : prune
Active --> [*] : remove
note right of Active
Normal ops: list / move
end note
classDef active fill:#e8f7ef,stroke:#15803d,color:#14532d
classDef locked fill:#fff1d6,stroke:#b45309,color:#713508
classDef stale fill:#fee2e2,stroke:#dc2626,color:#7f1d1d
class Active active
class Locked locked
class Stale stale
The commands below were verified on Git 2.50.1. Assume the main repository is at ~/repos/myapp on main.
1. Create a worktree
Create a sibling directory checked out to a new branch based on main:
git worktree add -b feat/login ../myapp-login main
Omitting -b and passing only a path (git worktree add ../myapp-hotfix) creates a branch named after the last path component (myapp-hotfix). To inspect a branch or commit without creating a branch, use detached HEAD:
git worktree add --detach ../myapp-review origin/main
2. List and inspect worktrees
git worktree list
Git lists the main worktree first, followed by linked worktrees with their commit hashes and checked-out branches:
/Users/you/repos/myapp 706d3c9 [main]
/Users/you/repos/myapp-login 706d3c9 [feat/login]
Inside the linked directory, the .git file contains only the link back to the main store:
gitdir: /Users/you/repos/myapp/.git/worktrees/myapp-login
3. Move, lock, and repair paths
Because the main repository tracks each linked directory path, move worktrees with git worktree move rather than mv or drag-and-drop:
git worktree move ../myapp-login ../worktrees/myapp-login
If a folder was moved manually, run git worktree repair ../worktrees/myapp-login from the main checkout to relink the administrative files. For worktrees stored on removable or temporarily unmounted drives, prevent pruning with lock:
git worktree lock --reason "External drive workspace" ../myapp-login
git worktree unlock ../myapp-login
4. Remove and prune stale entries
Once a branch is merged or discarded, remove both the directory and its metadata:
git worktree remove ../myapp-login
Git blocks removal if uncommitted changes or initialized submodules remain unless -f (--force) is passed. If you deleted a folder manually with rm -rf, run git worktree prune (or git worktree prune -n for a dry run) to clean stale metadata under .git/worktrees/.
How Agent IDEs and Coding Agents Use Worktrees
Most coding agents and Agent IDEs wrap these Git commands into session workflows and add hooks for dependencies and ignored files:
| Tool | How to Start | Default Location | Environment Handling |
|---|---|---|---|
| Claude Code | claude --worktree <name> (-w) or /worktree |
.claude/worktrees/<name> inside repo |
Copies ignored files matching .worktreeinclude; subagents support isolation: worktree |
| Cursor | /worktree, /best-of-n, or Agents Window |
~/.cursor/worktrees or custom path |
Runs setup commands in .cursor/worktrees.json (setup-worktree) |
| OpenAI Codex | Worktree mode in app; --worktree or /worktree in CLI |
$CODEX_HOME/worktrees |
Supports handoff between local checkout and worktree, plus setup scripts |
| Orca | One Git worktree per workspace | Managed by Orca | Pairs terminal, editor, browser preview, and diff annotations; supports sharing/copying ignored files |
| T3 Code | Per-thread Git worktree isolation | Managed on host | Tracks turn checkpoints so individual round diffs can be reviewed or reverted |
For a Node.js project in Cursor, .cursor/worktrees.json can reference $ROOT_WORKTREE_PATH to install packages and copy .env:
{
"setup-worktree": [
"npm ci",
"cp $ROOT_WORKTREE_PATH/.env .env"
]
}
In Claude Code, placing a .worktreeinclude file (using .gitignore pattern syntax) at the repository root copies matching ignored files into each new worktree:
.env
.env.local
config.local.json
See the site guides on Orca and T3 Code for how those workspaces pair worktrees with permissions and review tools.
Isolation Boundaries and Common Gotchas
Worktrees separate files and Git state, not the rest of the local runtime. This breakdown maps what Git handles natively versus what requires setup:
---
config:
theme: base
themeVariables:
git0: "#dbeafe"
gitBranchLabel0: "#132b55"
cScale0: "#dbeafe"
cScaleLabel0: "#132b55"
cScale1: "#e8f7ef"
cScaleLabel1: "#14532d"
cScale2: "#fff1d6"
cScaleLabel2: "#713508"
lineColor: "#64748b"
---
mindmap
Worktree Boundaries
Isolated by Git
Working tree files
HEAD and branch pointer
Own staging index
Needs Extra Setup
Ignored files omitted
.worktreeinclude or setup scripts
Ports & DBs shared
Separate env vars per workspace
Submodule limits
Update per tree; no move
Four practical details matter in daily use:
- Credential scope when copying
.env: Fresh worktrees omit ignored files; check which secrets live in.envbefore copying it automatically into agent directories. - Port and database collisions: Two agents running local servers on port
3000or writing to the same local database file will still collide unless separated by environment variables. - Repositories with Git submodules: Repositories using submodules (see Git Submodule Check and Fix Guide) do not populate submodule files automatically in a new worktree; run
git submodule update --init --recursiveinside the new directory. Git also prohibitsgit worktree moveon worktrees containing submodules, andgit worktree removerequires--forcewhen submodules are initialized. - Routine cleanup: Leaving old worktrees around keeps duplicated build artifacts on disk and keeps their branches checked out so the main workspace cannot switch to them. Remove finished worktrees with
git worktree removeand delete merged branches regularly.
This article is based on checks against Git 2.50.1 and official tool documentation on November 26, 2026. Default worktree locations, setup hooks, and cleanup prompts in agent tools change across releases; verify against your installed version’s documentation before automating workflows.
Sources:
- git-worktree Documentation (Git SCM, checked 2026-11-26)
- Common workflows - Run parallel Claude Code sessions with Git worktrees (Anthropic Claude Code Docs, checked 2026-11-26)
- Worktrees Configuration (Cursor Docs, checked 2026-11-26)
- Worktrees - Codex Environments (OpenAI / ChatGPT Learn, checked 2026-11-26)
- Worktrees Lifecycle (Orca Docs, checked 2026-11-26)
Author: Cyber Herald
Original URL: https://torchtree.com/en/post/git-worktree-agent-ide-guide/
Publish Date: 2026-11-26
Last Updated: 2026-11-26
License: CC BY-NC-SA 4.0