Software Engineeringunpacked

Git worktree explained: work on several branches at once (and run AI coding agents in parallel)

A git worktree is an extra working directory attached to the same repository. Each worktree has its own checked out branch, its own staged changes and its own files on disk, but they all share one history and one object store. So you can keep a feature half done in one folder, fix a bug on another branch in a second folder, and never stash or switch. You create one with git worktree add, see them all with git worktree list and delete one with git worktree remove. The one rule: a branch can be checked out in only one worktree at a time.

Video · Git worktrees: run AI coding agents in parallel, in under a minute · 0:46

Git worktree in one paragraph

Prefer to watch? The one-minute Short: Git worktrees: run AI coding agents in parallel, in under a minute. This guide goes further.

You are halfway through a feature. Files are edited, some are new, nothing compiles yet. Then a bug report lands and it needs a fix on main right now. The usual answer is git stash, git switch main, fix, switch back, git stash pop, and hope nothing collides. Git worktree offers a calmer way: leave the feature exactly where it is and open a second folder of the same repository, already checked out on another branch. Fix the bug there, commit, push, delete the folder. Your feature never moved.

The problem: one working directory, one branch

A normal clone gives you one working directory. It can show one branch at a time, so every context switch goes through it:

The Git documentation describes this exact moment in its example: your working tree is "in such a state of disarray (with new, moved, and removed files, and other bits and pieces strewn around) that you don't want to risk disturbing any of it." Its answer is a worktree.

What is a git worktree?

In the words of the docs, "a git repository can support multiple working trees, allowing you to check out more than one branch at a time." A worktree is a working directory plus a little metadata that tells it apart from the others.

There are two kinds:

Each worktree has its own HEAD, its own index (staging area) and its own files on disk. Everything else is shared: the object store, every branch and tag under refs/, the remotes and the configuration. A commit you make in one worktree is instantly visible from all the others, because there is only one repository underneath. Nothing is copied or downloaded twice.

my-app/            main worktree     branch: feature-search
my-app-hotfix/     linked worktree   branch: hotfix-login
my-app-review/     linked worktree   detached at a PR commit
        \______ one shared .git: objects, branches, remotes ______/

Git worktree commands, one by one

git worktree add

git worktree add ../my-app-hotfix

This creates the folder ../my-app-hotfix and checks something out in it. With no branch given, the docs say the new worktree gets a branch named after the last part of the path: here my-app-hotfix. If that branch does not exist, Git creates it from your current HEAD. If it exists and is not checked out anywhere else, Git checks it out.

You will usually be more explicit:

git worktree add ../my-app-hotfix -b hotfix-login main   # new branch from main
git worktree add ../my-app-old release-2.3               # existing branch
git worktree add -d ../my-app-review origin/main         # detached HEAD

-b <new-branch> creates a new branch starting at the commit you name (or HEAD if you name none), and refuses if that branch already exists. -B resets the branch instead of refusing, so use it with care. -d (or --detach) checks out a commit without any branch: the docs call this a throwaway worktree, handy for reading or testing code you do not plan to commit to.

The one rule: a branch lives in one worktree

By default, git worktree add "refuses to create a new worktree when <commit-ish> is a branch name and is already checked out by another worktree." So if main is checked out in your main folder, you cannot check out main again in a linked worktree. --force overrides the safeguard, but two folders moving the same branch pointer is a recipe for surprises. Make a new branch, or use --detach.

git worktree list

git worktree list

This shows every worktree: the main one first, then the linked ones, each with its path, the commit it has checked out and its branch (or "detached HEAD"). It also marks a worktree as locked or prunable. For scripts, git worktree list --porcelain gives a format the docs promise will stay stable across Git versions; they recommend adding -z.

git worktree remove

git worktree remove ../my-app-hotfix

This deletes the folder and its metadata. It only works on clean worktrees, which the docs define as "no untracked files and no modification in tracked files". --force removes an unclean one, and you need --force twice for a locked one. The main worktree cannot be removed. Note that removing a worktree does not delete its branch; do that with git branch -d once the branch is merged.

git worktree prune

git worktree prune -n    # show what would be pruned
git worktree prune       # clean up

If you delete a worktree folder by hand (with rm -rf or your file manager), Git still keeps its administrative files. prune removes the information for worktrees "whose working trees are missing". Git also cleans these up eventually by itself (see gc.worktreePruneExpire in git config). The docs add a gentle hint: use git worktree remove next time.

git worktree lock, unlock, move and repair

Git worktree example 1: a hotfix while a feature is half done

This is the docs' own scenario, with real names. You are in ~/code/my-app on feature-search, with a messy tree. Create the worktree, fix the bug and run the tests in it, then commit:

git worktree add ../my-app-hotfix -b hotfix-login main
cd ../my-app-hotfix
git commit -am "Fix login redirect loop"
git push -u origin hotfix-login
cd ../my-app
git worktree remove ../my-app-hotfix

You never touched feature-search. Your editor tabs, your unsaved experiments and your running dev server in my-app stayed exactly as they were. Once the hotfix is merged, bring it into your feature branch the normal way, with a merge or a rebase (see git merge vs rebase for which to pick).

Git worktree example 2: review a pull request without disturbing your work

A colleague asks you to try their branch. Instead of committing your half done work, open it next to yours:

git fetch origin
git worktree add -d ../my-app-review origin/their-branch
cd ../my-app-review
npm install && npm test

The -d keeps it detached, so you do not create a local branch you will forget about. When you are done, git worktree remove ../my-app-review. If you want to push small fixes to their branch, create a local branch instead (-b their-branch origin/their-branch).

Running AI coding agents in parallel with git worktrees

Worktrees have become the standard way to run two or more AI coding agents at the same time on one repository. An agent such as Claude Code edits files, runs commands and commits. Put two of them in the same folder and they step on each other: one rewrites a file the other is halfway through, one agent's failing test run picks up the other's broken change, and nobody can tell whose diff is whose.

Give each agent its own worktree and the problem goes away:

git worktree add ../my-app-agent-a -b agent/search-filters main
git worktree add ../my-app-agent-b -b agent/export-csv main

Start one agent session in each folder; Claude Code can also run unattended like this in CI, as our guide to Claude Code in GitHub Actions shows. Each one:

When an agent is done, review its branch like any pull request and merge it through the normal flow. Our Claude Code best practices guide recommends exactly this kind of setup for parallel sessions, and it pairs well with a separate reviewer session. Inside one session, Claude subagents split a task between helpers; worktrees are the outer layer that keeps whole sessions apart on disk.

What to watch for with parallel agents

Git worktree vs clone vs stash

git worktree second git clone git stash
Branches at once several, one per folder several, one per clone one
Disk use one shared object store plus files a full copy of every object none extra
History and branches shared instantly separate; sync with fetch and push same repository
Untracked files kept in place yes, in each folder yes, in each clone only with -u
Same branch in two places refused by default allowed (separate repos) not applicable
Best for parallel work, reviews, AI agents fully isolated experiments a quick pause of a few minutes

Stash is still fine for a thirty second interruption. A worktree wins once the switch lasts longer than a coffee, or when two things must run at the same time.

Common git worktree mistakes and cleanup

Git worktree checklist

  1. Commit or leave your current work as it is; no stash needed.
  2. git worktree add ../<repo>-<task> -b <branch> main.
  3. Install dependencies and copy local config into the new folder.
  4. Give it its own port if it runs a server.
  5. Work, commit and push from that folder.
  6. Merge through a normal pull request, with your test suite running on it (see types of software testing).
  7. git worktree remove ../<repo>-<task>, then delete the merged branch.

Sources: git worktree documentation · git stash documentation · git clone documentation

FAQ

What is a git worktree?

A git worktree is a working directory linked to a repository, with its own checked out branch, HEAD and index. The Git documentation says a repository can support multiple working trees, allowing you to check out more than one branch at a time. The folder created by git clone or git init is the main worktree; the ones you add with git worktree add are linked worktrees.

What is the difference between git worktree and git clone?

A clone is a second, independent repository with its own copy of every object, its own branches and its own remotes. A worktree is another working directory of the same repository: commits, branches and remotes are shared, so a commit made in one worktree is visible from all the others right away, and nothing is downloaded twice.

Can the same branch be checked out in two worktrees?

Not by default. git worktree add refuses to create a worktree when the branch is already checked out by another worktree. The --force option overrides that safeguard, but two folders moving the same branch tend to confuse each other, so create a new branch or use --detach instead.

How do I delete a git worktree?

Run git worktree remove <path>. It only removes clean worktrees, with no untracked files and no changes to tracked files; add --force to remove an unclean one. If you already deleted the folder by hand, run git worktree prune to clean up the leftover administrative files. The branch itself is not deleted.

Do I need to reinstall dependencies in each worktree?

Usually yes. A new worktree gets the tracked files of its branch, not ignored folders such as node_modules, build output or local .env files. Install dependencies and copy any local config into each worktree before you run it.