Beyond Git Worktree

Guide

git worktree add: every form of the command, with examples

How git worktree add works: create a worktree on a new branch, an existing branch, a remote branch or a detached HEAD, the options that matter, and how to fix the errors it throws.

Updated

git worktree add checks out a second (or tenth) copy of your repository's files into another directory, attached to the same repository. Every worktree has its own HEAD, index and working files, but they all share one object database and one set of branches, so there is nothing to clone and nothing to sync.

That makes it the cheapest way to have two branches open at once: a hotfix next to a half-finished refactor, a pull request under review next to your own work, or one isolated checkout per coding agent. This guide covers each form of the command, the options worth knowing, and what to do when it refuses.

Syntax

git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]
                 [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]
From the git-worktree manual.

<path> is the directory to create; it must not exist yet, or must be empty. <commit-ish> is what to check out there: a branch, a tag, a commit or a remote-tracking branch. Leave it out and Git picks a branch for you from the last part of the path, as below.

Add a worktree on a new branch

The shortest form names only the path. Git creates a branch named after the path's last component, based on your current HEAD, and checks it out there:

git worktree add ../hotfix
# creates branch 'hotfix' from HEAD, checked out in ../hotfix

If a branch called hotfix already exists and isn't checked out anywhere else, Git checks that branch out instead of creating one. To choose the branch name and the starting point yourself, use -b:

git worktree add -b feature/login ../app-login main
# new branch 'feature/login' starting at main, in ../app-login

-b refuses if the branch already exists. -B creates it or, if it exists, resets it to the starting point, which throws away whatever that branch pointed to before. Use -B only when that is what you mean.

Add a worktree for an existing branch

Pass the branch after the path:

git worktree add ../app-release release/2.4

A branch can be checked out in only one worktree at a time. If release/2.4 is already checked out in your main worktree, or in another linked one, the command stops. Switch that worktree to something else first, or see the errors below for the alternatives.

Add a worktree for a remote branch

git worktree add doesn't fetch. Fetch first, then name the branch without the remote prefix:

git fetch origin
git worktree add ../review-fix-timeouts fix-timeouts

When no local fix-timeouts exists but exactly one remote has a branch by that name, Git treats this as git worktree add --track -b fix-timeouts ../review-fix-timeouts origin/fix-timeouts: it creates a local branch that tracks the remote one. If several remotes have the branch, set checkout.defaultRemote=origin or spell it out:

git worktree add --track -b fix-timeouts ../review-fix-timeouts origin/fix-timeouts

With --guess-remote (or worktree.guessRemote = true in your config), the path-only form does the same: git worktree add --guess-remote ../fix-timeouts bases the new branch on the matching remote-tracking branch instead of on HEAD.

Add a throwaway worktree (detached HEAD)

To build, test or bisect without creating a branch, detach HEAD:

git worktree add -d ../scratch          # at the current commit
git worktree add -d ../v2.3-build v2.3.0 # at a tag

A detached worktree can sit on a commit that is also checked out elsewhere, so this is also the way to get a second copy of main without fighting the one-branch-one-worktree rule. Commits made there belong to no branch; create one with git switch -c <name> before you leave if you want to keep them.

Options worth knowing

OptionWhat it does
-b <branch>Create a new branch at <commit-ish> (default HEAD) and check it out. Fails if the branch exists.
-B <branch>Like -b, but resets the branch to <commit-ish> if it already exists.
-d, --detachCheck out with a detached HEAD, no branch.
-f, --forceOverride the safeguards: check out a branch already checked out elsewhere, or reuse a registered path that has gone missing. Give it twice for a locked path.
--no-checkoutRegister the worktree without checking out files, so you can set up sparse checkout first.
--lock [--reason <text>]Lock the new worktree as it is created, so prune leaves it alone (for worktrees on removable or network drives).
--track / --no-trackSet, or don't set, the starting branch as the new branch's upstream.
--guess-remoteWith a path only, base the new branch on a remote-tracking branch of the same name.
--orphanStart the worktree on a new branch with no history and an empty index.
--relative-pathsLink the worktree with relative paths, so the repository and its worktrees can be moved together.
-q, --quietSuppress feedback messages.
Some of these options are newer than others (--orphan and --relative-paths among them). Run git --version and check git worktree -h or the manual for your version before relying on one in a script.

When git worktree add refuses

The exact wording varies between Git versions, but the refusals come down to a handful of cases:

Error saysWhyFix
the branch is already checked out, or already used by worktree at <path>One branch, one worktree.Run git worktree list to find it. Use -d for a detached copy, -b for a new branch from it, or switch the other worktree away. --force works but two checkouts of one branch is how work gets overwritten.
<path> already existsThe target directory exists and isn't empty.Pick a new path, or empty the directory if it is truly unused.
a branch named <branch> already existsYou used -b with a name that is taken.Drop -b to check out the existing branch, choose another name, or use -B to reset it.
<path> is a missing but already registered worktreeThe directory was deleted by hand, but Git still has its record.Run git worktree prune and try again, or add -f.
invalid referenceThe branch or commit isn't known locally.git fetch first, and check the spelling with git branch -a.

What a new worktree doesn't bring with it

A worktree gets the tracked files of the commit it checks out, nothing else. Plan for these before the first build fails:

  • Untracked and ignored files: .env and other local config, node_modules, virtualenvs, build output and caches. Install dependencies and copy or generate local config in each worktree.
  • Ports and databases: two worktrees running the same dev server or test database will collide. Give each one its own port and database name.
  • Per-worktree config: repository config is shared by every worktree. For settings that should differ, turn on extensions.worktreeConfig and use git config --worktree.
  • Submodules: the manual still calls multiple checkouts of a superproject experimental and recommends against it.

Hooks are shared too, since they live in the common Git directory. If they assume a single checkout, they will misbehave in a second one.

Where to put worktrees

Keep them outside the main working tree. A worktree created inside it shows up in git status as an untracked directory unless you ignore it, and tools that walk the tree (test runners, file watchers, search) will find every file twice. Two common layouts:

# siblings of the main checkout
~/code/app            # main worktree
~/code/app-hotfix     # git worktree add ../app-hotfix
~/code/app-review-42  # git worktree add ../app-review-42 pr-42

# a bare repository with every branch as a worktree
~/code/app/.bare
~/code/app/main       # git worktree add main
~/code/app/feature-x  # git worktree add -b feature-x feature-x main

Whichever you choose, name the directory after the branch or task so git worktree list reads at a glance.

One worktree per coding agent

Coding agents running side by side need the same thing people do: their own files, their own branch, one shared history. A worktree per session gives each agent that without a full clone:

git fetch origin
git worktree add -b agent/fix-flaky-test ../wt/fix-flaky-test origin/main
cd ../wt/fix-flaky-test
# install dependencies, then start the agent here

Starting from origin/main rather than your local HEAD keeps every agent on the same known base. Some agent tools create worktrees for you; Claude Code, for one, documents running parallel sessions in worktrees at code.claude.com/docs/en/worktrees. Check your tool's own docs for where it puts them and whether it removes them afterwards.

List, remove and prune

git worktree list              # every worktree, its commit and branch
git worktree remove ../hotfix  # delete the directory and Git's record of it
git worktree prune             # forget worktrees whose directories are gone

remove refuses a worktree with modified or untracked files unless you pass --force, and the main worktree can't be removed. Removing a worktree doesn't delete its branch; do that with git branch -d once it has been merged. The git worktree remove guide, linked below, covers every case, including worktrees deleted by hand.

Frequently asked questions

Does git worktree add copy the repository?

No. It checks out files into a new directory and records the worktree in the repository's .git/worktrees/ directory. Objects, branches and tags are shared, so a commit made in one worktree is visible from all of them at once.

Can two worktrees have the same branch checked out?

Not by default: Git refuses so that two checkouts don't overwrite each other's work on one branch. Use a detached worktree (-d) or a new branch instead. --force overrides the check if you really need it.

What branch does git worktree add use if I don't name one?

One named after the last part of the path. git worktree add ../hotfix checks out an existing hotfix branch if it is free, and otherwise creates hotfix from the current HEAD.

Does git worktree add fetch from the remote?

No. It works from the refs you already have, so run git fetch first when the branch you want is new on the remote.

How do I delete a worktree?

Run git worktree remove <path>. If you already deleted the directory by hand, run git worktree prune to clear Git's record of it.

Sources

More on worktrees in Git Worktrees, or browse all guides.

Further reading in Git Worktrees