Skip to content

Gondew Life

AI·ML 개발, 맥 생산성, 웹 디자인 기록

Menu
  • English
  • 한국어
  • about
  • Contact
  • Privacy
Menu

[2026 Guide] Running AI Coding Agents in Parallel with Git Worktree (Claude Code, Codex, and Antigravity)

Dec 4, 20262026년 12월 04일
이 글은 한국어로도 읽을 수 있습니다.한국어 버전으로 읽기 →
Key Takeaways (TL;DR): Running multiple AI coding agents (Claude Code, OpenAI Codex, and Google Antigravity) inside a single Git working directory causes file overwrites, broken test runs, and polluted staging areas. Git Worktree solves this by sharing one lightweight .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.

AspectSingle Working DirectoryMultiple git clone Copiesgit worktree Parallel Setup
File & staging isolationNone (agents overwrite each other)Full isolationFull isolation (separate tree, index, HEAD)
.git history storageSingle copyDuplicated per cloneShared single database (lightweight)
Local commit visibilityImmediateRequires push / fetchImmediate across all worktrees (diff, cherry-pick, merge)
Creation speedN/ASlow (network + full copy)Instant local checkout
Same-branch guardN/AAllows accidental duplicate checkoutsBlocks 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 polluting git status and causing AI search tools to index duplicate copies of every file.
  • Use git worktree lock for long-running sessions: Locking an active worktree (git worktree lock ../my-app-wt/oauth --reason "Agent running") prevents accidental cleanup via prune.

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:

CapabilityClaude Code (Anthropic)OpenAI Codex (App & CLI)Google Antigravity (App · IDE · agy)
Native worktree modeclaude --worktree <name> (-w)App Worktree composer mode / CLI --worktree or --cdApp/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 behaviorAuto-creates dedicated branch (e.g. worktree-<name>)Starts in detached HEAD → promote to branch on handoffDedicated agent branch per isolated workspace
Ignored files (.env)Auto-copies files matched by root .worktreeincludeAuto-copies .worktreeinclude + AGENTS.override.mdShell helper / hook copy or project-level config
Manual worktree CLIcd <worktree> && claudecodex --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

  1. Missing .env files: Git does not copy gitignored files into a new manual worktree. Copy .env explicitly (or define .worktreeinclude for native agent worktrees).
  2. Bloated node_modules / Python virtualenvs: Prefer content-addressable or hardlink-backed managers like pnpm (JS/TS) and uv (Python) so 3–5 worktrees share cached packages on macOS APFS without multiplying disk usage.
  3. 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.
  4. One-command shell helpers: Add tiny wt-ai and wt-rm functions to ~/.zshrc so spinning up a clean worktree with .env copied 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.

글 탐색

← [2026년 최신] Git Worktree로 AI 코딩 에이전트(Claude Code·Codex·Antigravity) 병렬 실행하는 실전 워크플로우
Brewfile과 brew bundle로 새 맥북 개발 환경 5분 만에 복원하기 (Apple Silicon 트러블슈팅 포함) →
© 2026 Gondew Life | Powered by Minimalist Blog WordPress Theme