DX Heroes logo
#ai
#developer-experience
#engineering

What is a git worktree?

Length: 

6 min

Published: 

November 25, 2026

What is a git worktree?

What is a git worktree?

A git worktree is a working directory attached to an existing Git repository. The Git documentation sums it up in one sentence: "A git repository can support multiple working trees, allowing you to check out more than one branch at a time."

Every repository has one main worktree, the one that git clone or git init created. You can add any number of linked worktrees with git worktree add. A linked worktree shares everything with the repository except per-worktree files such as HEAD and the index. Each worktree has its own files on disk and its own checked-out branch, while commits, branches and remotes live in one place. A commit made in one worktree is visible in all the others straight away, with no push and no fetch.

The command arrived in Git 2.5 in July 2015, marked as experimental at the time. For years few developers used it. In June 2026 the GitHub blog wrote that worktrees "have been around since 2015, but it wasn't until recently they became popular". The same post adds: "Agents and humans can do more in parallel with worktrees."

In plain words

Picture a workshop with a single workbench. To start a second job, you have to clear the bench and pack the first job away, then unpack it again later. A worktree adds a second bench to the same workshop. Both benches take parts from the same store, and each one holds its own job in progress. Two people, or two AI agents, can work side by side and never knock each other's work off the table.

How it works

A few commands cover most of the daily use:

# create branch "hotfix" and check it out in ../hotfix
git worktree add ../hotfix

# check out an existing branch in a new directory
git worktree add ../review-login feature/login

# list all worktrees, then remove one when you are done
git worktree list
git worktree remove ../hotfix

The rules worth knowing:

  • One branch, one worktree. By default, git worktree add refuses to check out a branch that another worktree already has checked out. Two directories never write to the same branch.
  • Only a clean worktree can be removed. git worktree remove deletes a worktree only when it has no untracked files and no changes in tracked files, unless you add --force. The main worktree cannot be removed.
  • A directory deleted by hand leaves traces. The repository keeps the worktree's administrative files until Git prunes them on its own schedule, or until you run git worktree prune.
  • Each worktree is a fresh checkout. Files that Git ignores, such as node_modules, build output or a local .env, are missing in a new worktree. You set up dependencies in each one again.

Why AI coding agents use worktrees

An AI coding agent edits files, runs tests and makes commits. Two agents in one directory would edit the same files and switch branches under each other. A worktree gives each agent its own copy of the files, while all of them share one repository. Running several agents at once in agentic coding depends on that separation.

The main tools build on it:

  • Claude Code starts a session in its own worktree with claude --worktree <name>. By default it creates the worktree under .claude/worktrees/<name>/ on a new branch named worktree-<name>. A custom subagent with isolation: worktree in its definition always runs in a worktree of its own (Claude Code docs).
  • Cursor 2.0, released in October 2025, runs many agents in parallel "without them interfering with one another, powered by git worktrees or remote machines".
  • The GitHub Copilot app offers a new worktree as the default place for a new session, according to the GitHub blog.

Each agent works on its own branch, and its result comes back as an ordinary branch or pull request. You review each one on its own, as you would a colleague's work.

Worktrees help people without any agent too. An urgent bug can get its own worktree while a half-finished feature stays open in the first one. In the GitHub blog's words, "you never leave your branch and you never stash". That takes some of the cost out of context switching.

How we use worktrees

Our marketing and operations agents run as headless Claude Code sessions on a schedule, and their runs can overlap. At first, two of these scheduled jobs shared one checkout of our business repository. On 2 October 2026 they ran in it at the same time, and one run's git checkout switched the working tree under the other run mid-task. The other run lost nothing only because it had committed its report about a minute earlier. Since then our agents do their work in worktrees of their own, and that repository has more than 30 of them.

What we learned along the way:

  • Set up the environment right after git worktree add. A new worktree of our website repository has no node_modules and no generated next-env.d.ts, so the first build fails for reasons that look like a broken repository. A symlink to the main checkout's node_modules looked like a shortcut, and pnpm refused it. A hard-linked copy (cp -al) takes about five seconds and works.
  • Commit early and push later. All worktrees share one object store, so a commit is visible from every other worktree of the repository at once, pushed or not. Our content agents commit after each phase of an article. A run that gets cut off leaves a branch that the next run, in any worktree, can pick up.
  • Run git status before you start. A worktree can hold work that never reached a commit. One of our runs was stopped after it had written its files and before it committed them, and the finished work existed only in its worktree. Our agents check the worktree first and only then start a task again.

What to watch out for

  • Disk space and dependencies. Each worktree needs its own installed dependencies, which the GitHub blog calls "dependency bloat". Ten agents in ten worktrees mean ten copies of node_modules.
  • Cleanup. Worktrees pile up. At the end of an interactive session, Claude Code removes the clean worktree of an unnamed session on its own and asks you in the other cases. After a non-interactive run with -p, it leaves the worktree in place, and you remove it with git worktree remove. A clean worktree whose branch has since been merged holds nothing you would lose, so you can remove it. One with uncommitted changes needs a look before you delete it.
  • Ignored and secret files. Worktrees created inside the repository belong in .gitignore. Claude Code recommends adding .claude/worktrees/ there. A new worktree has no copy of your .env, and Claude Code's .worktreeinclude file lists the ignored files to copy into each one.
  • Shared configuration and machine. Worktrees share the repository's config file, branches and remotes. On one machine they also share ports, databases and other local services, so two agents that start a dev server on the same port still collide.
  • Merge conflicts arrive later. Worktrees keep agents apart while they work. When their branches change the same part of the same file, the conflict shows up at merge time. Two of our own knowledge-base pull requests, open at the same time on separate branches, conflicted in a shared image manifest.
  • One task at a time needs no worktree. For a single line of work, a branch and git switch are simpler. The GitHub blog answers "Should I use worktrees?" with "It depends". We add a worktree once two or more jobs run at the same time.

Related articles:

Want to stay one step ahead?

Don't miss our best insights. No spam, just practical analyses, invitations to exclusive events, and podcast summaries delivered straight to your inbox.