All posts
AI Engineering

Git Worktrees, Now That Your Coding Agent Uses Them: How They Work and What Breaks

By

November 2, 2026

6 min read

Claude Code runs parallel sessions and subagents in git worktrees: separate checkouts on separate branches, sharing one repository. What a worktree shares and doesn't, the one-branch rule, where Claude Code puts them and when it cleans them up, and the things that still collide: dependencies, ports, local databases and .env files.

Git Worktrees
Claude Code
AI Coding Agents
Git
Developer Tools

A git worktree is a second working directory for the same repository: its own files, its own branch, its own staging area, the same history. Coding agents use them so two sessions, or a session and its subagents, can edit at the same time without overwriting each other. If you run Claude Code with --worktree, or ask it to use worktrees for its agents, this is what is happening underneath.

What is shared and what isn't

Shared by every worktree

Separate in each worktree

Commits, branches and tags (all refs under refs/)

HEAD: which commit or branch is checked out

Repository config (unless worktree config is enabled)

The index: what is staged

Remotes, hooks and the object store

The files on disk, including untracked and ignored ones

Two consequences follow. A commit made in one worktree is visible from all of them at once, with no push or fetch. And git refuses to check out a branch that another worktree already has checked out, so each worktree works on its own branch, and merging is how their work comes together.

Want a second opinion on this for your system?

Book a free 30-minute call. You'll leave knowing what we'd do first and roughly what it costs, whether or not you hire us.

How Claude Code uses them

  • `claude --worktree feature-auth` (or `-w`) creates `.claude/worktrees/feature-auth/` on a new branch, `worktree-feature-auth`, and starts the session there. Run it again with another name in another terminal for a second, isolated session.

  • New worktrees branch from the repository's default branch on the remote. Set `worktree.baseRef` to `"head"` to branch from your current local work instead, which is what subagents working on an in-progress change usually need.

  • Subagents get their own worktrees when you ask for it, or always, with `isolation: worktree` in a custom agent's frontmatter.

  • On exit, a clean worktree from an unnamed session is removed with its branch; one with changes, untracked files or new commits prompts you to keep or remove it. Subagent worktrees with work in them stay until a periodic sweep can remove them without losing anything.

  • While a session is isolated, Claude Code refuses edits and git commands aimed at the main checkout.

What still collides

Problem

Why

Fix

Missing .env and local config

A worktree is a fresh checkout; ignored files are not copied

List them in a .worktreeinclude file (gitignore syntax); Claude Code copies matching ignored files into new worktrees

Dependencies

node_modules, virtualenvs and build output live in the working directory

Install per worktree, or use a package manager with a shared store (pnpm) to save disk

Ports

Two dev servers both want 3000

Take the port from an environment variable and set a different one per worktree

The local database

Two branches run different migrations against one Postgres

A database per worktree (a name suffix is enough), or a throwaway container each

Docker Compose

Each worktree gets its own project name from its directory, but fixed container names and published host ports still collide

Drop container_name, and take published ports from per-worktree environment variables

Untracked clutter in the main checkout

.claude/worktrees/ sits inside the repository

Add .claude/worktrees/ to .gitignore

Submodules and LFS

Git calls multiple checkouts with submodules experimental; repo-local LFS filters are skipped by Claude Code

Run git lfs pull in the worktree; avoid worktrees on superprojects with submodules

Housekeeping commands

  • `git worktree list` shows every worktree and its branch.

  • `git worktree remove <path>` deletes one cleanly (it must be clean, or use --force). Deleting the directory by hand leaves metadata behind; `git worktree prune` clears it.

  • `git worktree lock` protects one from pruning; Claude Code locks the worktrees of running agents, so unlock before removing one by hand.

  • `git worktree repair` reconnects worktrees after you move the repository.

hoolco Monthly Review

Once a month: what changed in tech that month and what it means for you, from cloud and AI to security and the tools you build with. Every item links to its source. No sales pitch, unsubscribe in one click.

As of November 2, 2026. Provided as is, for general information, and accurate to the best of our knowledge on the date shown. Not legal, compliance, tax, financial or other professional advice. Prices, limits and vendor services change; check current vendor documentation before relying on anything here. Terms of use.