.git database across isolated sibling directories on separate branches—letting you run feature work, refactoring, and tests in parallel without conflicts.Modern AI coding agents no longer stop at inline autocomplete—they edit multiple files, run build pipelines, and execute test suites autonomously. Naturally, developers want to run two or three agents in parallel: one building a new feature, another refactoring a legacy module, and a third writing integration tests.
The catch is that running parallel agents inside one working directory quickly breaks down. Agent A edits package.json while Agent B runs tests; git status turns into a tangled mix of unrelated changes; and a single git add . or branch switch contaminates everything. Git Worktree eliminates this friction at the filesystem and Git level. This guide covers the core commands, how Claude Code, OpenAI Codex, and Google Antigravity support worktrees in 2026, a step-by-step 3-agent parallel workflow, and troubleshooting for .env, dependencies, and local ports.
1. Why Git Worktree Beats Single Directories and Multiple Clones
If you want a quick refresher on Git’s local repository and branching model first, check out Git vs GitHub Explained: Core Differences, Essential Commands, and PR Workflows. By default, one .git repository maps to one working tree. Git Worktree lets a single .git object database back multiple independent working directories, each checked out to a different branch with its own staging index.
| Aspect | Single Working Directory | Multiple git clone Copies | git worktree Parallel Setup |
|---|---|---|---|
| File & staging isolation | None (agents overwrite each other) | Full isolation | Full isolation (separate tree, index, HEAD) |
.git history storage | Single copy | Duplicated per clone | Shared single database (lightweight) |
| Local commit visibility | Immediate | Requires push / fetch | Immediate across all worktrees (diff, cherry-pick, merge) |
| Creation speed | N/A | Slow (network + full copy) | Instant local checkout |
| Same-branch guard | N/A | Allows accidental duplicate checkouts | Blocks checking out the same branch twice |
2. Four Essential Git Worktree Commands
# 1-1) Create an isolated worktree on a new branch off main
git worktree add -b feat/oauth-login ../my-app-wt/oauth main
# 1-2) Track an existing remote branch in a new worktree (avoids detached HEAD)
git worktree add --track -b hotfix/payment ../my-app-wt/hotfix origin/hotfix/payment
# 2) Inspect all active worktrees and lock states
git worktree list --verbose
# 3) Safely remove a worktree after merging (add -f to force)
git worktree remove ../my-app-wt/oauth
# 4) Clean stale metadata if a directory was removed manually
git worktree prune --dry-run
git worktree prune --verbose
- Keep worktrees in a sibling directory (
../<repo>-wt/): Nesting worktrees inside your main repo risks pollutinggit statusand causing AI search tools to index duplicate copies of every file. - Use
git worktree lockfor long-running sessions: Locking an active worktree (git worktree lock ../my-app-wt/oauth --reason "Agent running") prevents accidental cleanup viaprune.
3. Native Worktree Support: Claude Code vs. Codex vs. Antigravity (2026)
For a deeper look at plugins and skills across these three platforms, see Claude Code Setup vs OpenAI Codex vs Google Antigravity. Below is how each tool handles Git Worktree isolation:
| Capability | Claude Code (Anthropic) | OpenAI Codex (App & CLI) | Google Antigravity (App · IDE · agy) |
|---|---|---|---|
| Native worktree mode | claude --worktree <name> (-w) | App Worktree composer mode / CLI --worktree or --cd | App/IDE New Worktree Mode & multi-agent workflows (/teamwork, run_workflow) |
| Default path | .claude/worktrees/<name>/ | $CODEX_HOME/worktrees (~/.codex/worktrees) | Managed agent worktree or any manual worktree opened with agy |
| Branch behavior | Auto-creates dedicated branch (e.g. worktree-<name>) | Starts in detached HEAD → promote to branch on handoff | Dedicated agent branch per isolated workspace |
Ignored files (.env) | Auto-copies files matched by root .worktreeinclude | Auto-copies .worktreeinclude + AGENTS.override.md | Shell helper / hook copy or project-level config |
| Manual worktree CLI | cd <worktree> && claude | codex --cd <worktree> | cd <worktree> && agy |
When mixing Claude Code, Codex, and Antigravity on the same repository, creating manual sibling worktrees under ../<repo>-wt/<task> gives you one unified view via git worktree list regardless of which agent owns each task.
4. Step-by-Step: Running 3 AI Agents in Parallel
# 1) Add .worktreeinclude in repo root (safe to commit: filenames only, no secrets)
cat <<'EOF' > .worktreeinclude
.env
.env.local
EOF
# 2) Sync latest main and spin up 3 isolated sibling worktrees
cd ~/dev/my-saas-app
git fetch origin main
mkdir -p ../my-saas-app-wt
git worktree add -b feat/oauth-login ../my-saas-app-wt/oauth origin/main
git worktree add -b refactor/api-client ../my-saas-app-wt/refactor origin/main
git worktree add -b test/checkout-flow ../my-saas-app-wt/tests origin/main
# 3) Launch each agent in its own terminal pane / tab
# [Pane 1] Claude Code — feature implementation
cd ~/dev/my-saas-app-wt/oauth && claude
# [Pane 2] OpenAI Codex CLI — typed refactor
codex --cd ~/dev/my-saas-app-wt/refactor --sandbox workspace-write \
"Refactor src/lib/api-client.ts for strict TypeScript types and keep all unit tests green"
# [Pane 3] Google Antigravity — test suite expansion
cd ~/dev/my-saas-app-wt/tests && agy
Once each agent finishes, review each diff independently with git diff origin/main --stat, open separate PRs via gh pr create, and remove merged worktrees with git worktree remove and git branch -d.
5. Four Real-World Pitfalls & Automation Helpers
- Missing
.envfiles: Git does not copy gitignored files into a new manual worktree. Copy.envexplicitly (or define.worktreeincludefor native agent worktrees). - Bloated
node_modules/ Python virtualenvs: Prefer content-addressable or hardlink-backed managers likepnpm(JS/TS) anduv(Python) so 3–5 worktrees share cached packages on macOS APFS without multiplying disk usage. - Local port collisions (
EADDRINUSE): Assign distinct ports per worktree (e.g.,PORT=3001,PORT=3002) when multiple agents run dev servers or Playwright tests concurrently. - One-command shell helpers: Add tiny
wt-aiandwt-rmfunctions to~/.zshrcso spinning up a clean worktree with.envcopied takes one second:
# Add to ~/.zshrc
wt-ai() {
local branch="$1"
local name="${branch##*/}"
local repo_root repo_name target
repo_root=$(git rev-parse --show-toplevel) || return 1
repo_name=$(basename "$repo_root")
target="$(dirname "$repo_root")/${repo_name}-wt/${name}"
mkdir -p "$(dirname "$target")"
git worktree add -b "$branch" "$target" origin/main || return 1
[ -f "$repo_root/.env" ] && cp "$repo_root/.env" "$target/.env"
[ -f "$repo_root/.env.local" ] && cp "$repo_root/.env.local" "$target/.env.local"
cd "$target" || return 1
}
wt-rm() {
local target="$1"
git worktree remove "$target" && git worktree prune
}
FAQ
Q. Can two Git worktrees check out the same branch at the same time?
No. By design, Git prevents checking out the same branch in two worktrees simultaneously so two agents cannot create conflicting HEAD states. Give each worktree its own feature, refactor, or test branch.
Q. Does Git Worktree double my repository’s disk footprint?
Not for Git history—all worktrees share one .git object database. Only tracked working files and untracked dependency folders (node_modules, .venv) occupy space, which stays minimal when paired with pnpm or uv.
Q. Should I use each agent’s built-in worktree mode or manual sibling worktrees?
If you only use one tool (such as claude -w), its built-in mode is fastest. If you combine Claude Code, Codex CLI, and Antigravity across the same codebase, manual sibling worktrees (../<repo>-wt/) keep every active branch and port visible in a single git worktree list.