Skip to content
woza.ink
← Notebook
8 min readWritten with AI

Part 4 of 6 in Agentic coding

Git Worktrees for Parallel Agent Work

Running multiple AI agents in parallel means running multiple branches in parallel. Git worktrees are how you do that without thrashing your working directory.

On this page
  1. What a worktree is
  2. Why this matters for agents
  3. The commands
  4. A concrete parallel agent workflow
  5. A few gotchas
  6. Shell alias worth having
  7. The bigger picture
  8. Worktrunk: the purpose-built CLI
  9. Further reading

The bottleneck in agentic coding isn't the agent's speed. It's yours. You have one working directory, and switching branches mid-task means stashing, checking out, context-switching back, and hoping you remember where you were.

Git worktrees fix this. They let you check out multiple branches simultaneously into separate directories, each with its own working tree. One repo, multiple branches, all live at once.

If you'd rather skip the raw git commands and use a purpose-built CLI, jump to the worktrunk section below.


What a worktree is

Normally your repo has one working tree — the directory where files exist on disk. A worktree is an additional checkout of the same repo at a different path, on a different branch1.

# your main working directory
~/projects/myapp/          # branch: main

# additional worktrees
~/projects/myapp-feat-auth/   # branch: feat/auth
~/projects/myapp-fix-bug-42/  # branch: fix/bug-42

All three point to the same .git directory. They share history, remotes, and refs. They don't share working files — each directory has its own copy of the branch it's checked out on.

Three directories, each on its own branch and each with its own agent, all point at the same .git directory. They share history, remotes and refs, and keep separate working files Agent 1 myapp/ main Agent 2 myapp-feat-auth/ feat/auth Agent 3 myapp-fix-bug-42/ fix/bug-42 One .git directory History, remotes and refs are shared Every branch live at once. Each agent has its own files, so nobody steps on anybody.
Three directories on three branches, one agent in each, all sharing one .git directory.

Why this matters for agents

When you're running cmux or any parallel terminal setup with multiple Claude Code sessions2, each agent needs its own branch3. If they share a working directory, they step on each other — one agent's file write conflicts with another's read, and your git state becomes a mess.

The pattern:

  1. Create a worktree per task
  2. Open each worktree in its own terminal tab / cmux pane
  3. Run an agent in each — they operate fully independently
  4. Review, merge, delete the worktree when done

The commands

Create a worktree:

git worktree add ../myapp-feat-auth feat/auth

This creates ../myapp-feat-auth/ checked out on feat/auth. If the branch doesn't exist yet:

git worktree add -b feat/auth ../myapp-feat-auth main

Creates the branch from main and checks it out into the new directory.

List all worktrees:

git worktree list

Output:

/Users/you/projects/myapp           abc1234 [main]
/Users/you/projects/myapp-feat-auth def5678 [feat/auth]

Remove a worktree when you're done:

git worktree remove ../myapp-feat-auth

This removes the directory and cleans up the internal bookkeeping. If the worktree has uncommitted changes it'll warn you; use --force to remove anyway.

Prune stale worktree records (e.g. after manually deleting the directory):

git worktree prune

A concrete parallel agent workflow

Say you're working on three things at once: a feature, a bug fix, and a refactor. Here's the setup:

# from your main repo directory
git worktree add -b feat/user-auth ../myapp-auth main
git worktree add -b fix/session-leak ../myapp-session main
git worktree add -b refactor/db-layer ../myapp-db main

Three directories, three branches4. In cmux, open each in its own tab. Launch an agent in each. They run concurrently with zero interference.

When an agent finishes:

# in the worktree directory, push the branch
git push -u origin feat/user-auth

# back in main repo, remove the worktree
git worktree remove ../myapp-auth

Then open a PR as usual.

A sketch of three branches leaving main at the same point. Each runs an agent in its own directory at the same time, then is pushed, its worktree removed and a PR opened, at different moments Three branches, all live at once A sketch: lengths aren't measured main add 3 worktrees feat/user-auth PR fix/session-leak PR refactor/db-layer PR Each bar is one agent in its own directory. Finish in any order.
Three branches leave main together and each finishes on its own schedule. A sketch.

A few gotchas

You can't check out the same branch in two worktrees. Git enforces this — it prevents two worktrees from getting out of sync on the same branch5. If you try, you get an error. The fix is always to work on separate branches.

Paths can get messy. I put worktrees in sibling directories (../myapp-auth) rather than subdirectories of the main repo, to avoid accidentally including them in file searches or editor indexing. Some people use a dedicated ~/worktrees/ folder.

Your editor might not know about them. If you open the main repo in VSCode or Cursor, it won't automatically show the worktree directories. Open each worktree as a separate workspace window. This is fine — it's actually the point. Each agent and each editor window has its own isolated context.

Stash crosses worktrees. Each worktree has its own index, but the stash is shared. A stash made in one worktree shows up in git stash list in all of them, and popping it applies it wherever you are, so don't pop what another agent stashed.

Inside the main .git directory, objects, refs including refs/stash, and config are shared by every worktree. A private folder under worktrees/ holds that worktree's own HEAD and index. The linked directory has a .git file pointing to that private folder, and its own working files What a linked worktree really is Most of it lives in the main .git myapp/.git/ Shared by all objects/ refs/heads/ refs/stash config Private to one worktrees/feat-auth/ HEAD index ../feat-auth/ .git A plain file Working files Its own copy
What's private to a worktree and what every worktree shares, stash included.

Shell alias worth having

If you're doing this frequently, a small alias saves typing6:

# in your .zshrc
gwt() {
  local branch="$1"
  local dir="${2:-../${PWD##*/}-${branch//\//-}}"
  git worktree add -b "$branch" "$dir" main
  echo "Worktree created at $dir"
}

Then:

gwt feat/payments
# creates ../myapp-feat-payments on branch feat/payments

The bigger picture

Worktrees are a relatively obscure git feature — most developers have never used them7. But once you're running agents in parallel, they become load-bearing infrastructure. The alternative (stash + checkout + re-stash) doesn't scale past one context switch.

Pair worktrees with cmux for terminal organization, and you have a clean model: one pane per task, one worktree per pane, one agent per pane. The overhead is three commands to set up and one to tear down.



Worktrunk: the purpose-built CLI

Raw git worktree commands are functional but verbose8. Creating a worktree requires typing the branch name twice plus a path. Switching means a cd. Cleaning up is three commands. It works, but the friction adds up when you're spinning up and tearing down worktrees dozens of times a day.

Worktrunk (wt) is a Rust CLI built specifically to fix this. 3.3k stars on GitHub, first released in late 2025, and already the default tool for this workflow in the agentic coding community.

Install:

brew install worktrunk && wt config shell install

The shell integration is what makes wt switch actually switch your working directory — without it, you'd need to cd manually after every command.

The core three commands:

# create a worktree + branch from main, switch to it
wt switch --create feat/auth

# list all worktrees with status (staged changes, unpushed commits)
wt list

# squash, rebase, merge, clean up — all in one
wt merge main

Compare this to plain git:

Task worktrunk plain git
Create + switch wt switch -c feat git worktree add -b feat ../repo.feat && cd ../repo.feat
Launch agent wt switch -c -x claude feat (above) + claude
Clean up wt remove cd ../repo && git worktree remove ../repo.feat && git branch -d feat
Characters to type for each task in the comparison table, to scale. worktrunk needs 17, 27 and 9 characters. Plain git needs 56, 66 and 68 Characters to type Counted from the commands in the table, to scale Create + switch wt 17 git 56 Launch agent wt 27 git 66 Clean up wt 9 git 68
The same three tasks, counted in characters.

Launching agents directly:

# create worktree and immediately launch Claude Code in it
wt switch -x claude -c feat/payments -- 'Implement Stripe webhook handler'
wt switch -x claude -c fix/session-leak -- 'Fix the session memory leak in auth middleware'

The -x flag runs a command after switching; everything after -- is passed to it as arguments. Two commands, two agents, two isolated branches, running in parallel.

Hooks for automation:

Worktrunk supports lifecycle hooks — run commands on create, pre-merge, post-merge. The main use cases: install dependencies automatically when a new worktree is created, start a dev server on a unique port per worktree (hash_port template filter gives each one a deterministic port so they don't collide).

wt list --full:

This is the underrated feature. Full mode shows CI status, AI-generated branch summaries, PR links, and whether each worktree is ahead/behind main. When you have six agents running, this is your dashboard.

The honest comparison:

Raw git worktrees and worktrunk get you to the same place. The question is whether the friction of the raw commands is worth avoiding. If you're running one or two parallel tasks occasionally, plain git is fine. If parallel worktrees are a daily workflow with five or more simultaneous agents, worktrunk pays for itself in the first hour.


Further reading

Notes

  1. 1Context

    Git worktree was introduced in Git 2.5, released July 27, 2015. The announcement described linked working trees as "pseudo-repositories" — each with its own checked-out working copy, whose .git is actually a file that refers back to the main repository's history and refs. The feature was marked experimental at launch and took years to gain mainstream awareness.

    source →
  2. 2Source

    Claude Code v2.1.49 (February 2026) shipped native --worktree (-w) flag support. Running claude --worktree name creates an isolated branch and directory at .claude/worktrees/name/. Subagents can also use isolation: worktree in their frontmatter to automatically get their own worktree on every run.

    source →
  3. 3Quote

    "AI agents like Claude Code and Codex can handle longer tasks without supervision, such that it's possible to manage 5-10+ in parallel." The rise of agentic coding has turned worktrees from a niche git feature into load-bearing infrastructure for parallel development.

    — Worktrunk documentation

    source →
  4. 4Note

    Mikhail Rogov documented scaling to 371 git worktrees (172 GB on disk) for multi-agent AI orchestration. Key lesson: for small and medium tasks, the coordination overhead of spawning, monitoring, and merging was actually slower than running agents sequentially with clear handoffs. He ended up reducing his agent count from 7 to 5 and replacing 6,460 lines of bash with 1,600 lines of Python.

    — Mikhail Rogov, "What 371 Git Worktrees Taught Me About Multi-Agent AI"

    source →
  5. 5Context

    The single-branch-per-worktree constraint exists because Git's ref system assumes one HEAD per working directory. If two worktrees shared a branch, a commit in one would silently move HEAD in the other, leaving it with a dirty diff it never created. This is also why git worktree lock was added in Git 2.10 — to prevent accidental cleanup of worktrees on network or removable drives.

  6. 6Counter

    Shell aliases work, but dedicated tools like worktrunk (wt) go further: computed paths from branch names, lifecycle hooks for auto-installing dependencies, deterministic port allocation via hash_port, and wt list --full showing CI status and PR links across all worktrees. If you're past 3-4 concurrent worktrees, the raw git + alias approach starts to show its limits.

    source →
  7. 7Quote

    "Local AI coding agents are drastically changing both how engineers write code, and how many tasks an engineer may have in progress at a time." Worktrees went from obscure to essential in under a year — driven almost entirely by the agentic coding workflow.

    — Nick Mitchinson

    source →
  8. 8Source

    OpenAI's Codex also adopted worktrees as a first-class concept. Their developer docs describe worktrees as the isolation primitive for parallel agent execution — the same pattern Claude Code uses with --worktree. The convergence across vendors suggests this is becoming the standard approach.

    source →