Skip to main content

What are Git Worktrees?

Git worktrees let one repository provide several working directories at the same time.

Introduction​

A Git worktree is a directory where Git checks out a commit so you can edit and test it. Every repository has a main worktree unless it is bare. git worktree add creates linked worktrees, so you can keep several branches open without a second clone.

Definition

Git Worktree

A working directory associated with a Git repository, containing checked-out files plus its own HEAD and index.

With worktrees you stop stashing unfinished work just to review a branch or patch production. Tools and coding agents running at the same time also get their own files to edit. Editors manage them too: see Git worktrees in VS Code and Zed worktrees.

Understanding the Concept​

The main and linked worktrees share one object database and most refs. Each worktree gets its own HEAD, index, and working files. A linked worktree holds a .git file that points at its own admin directory under .git/worktrees/<name> in the main repository.

Definition

Main Worktree

The working directory created when a non-bare Git repository is initialized or cloned.

Definition

Linked Worktree

An additional worktree registered with the same Git repository through git worktree add.

Because the repository is shared, a commit made in one worktree is visible at once from all of them. Local branches, tags, remote-tracking refs, the stash, and most repository config are shared too.

Git stops you checking out the same local branch in two worktrees. Otherwise both directories could move one branch ref from different working states.

Core Commands​

Every subcommand runs from inside any worktree of the repository. The table lists the ones you use day to day.

CommandWhat it does
git worktree add <path> [<commit-ish>]Create a linked worktree at <path>
git worktree add -b <branch> <path> [<start>]Create a new branch and check it out in a new worktree
git worktree listShow every worktree with its commit and branch
git worktree remove <path>Delete a clean linked worktree and its admin record
git worktree pruneDrop records for worktrees whose directory is gone
git worktree lock <path>Protect a worktree from prune, move, and remove
git worktree unlock <path>Lift the lock
git worktree move <path> <new-path>Move a linked worktree to a new directory

Add. git worktree add ../repo-feature feature checks out the existing branch feature. How to use git worktree add lists every form with its output. If you leave out the commit and no branch matches the directory name, Git creates a branch named after the last path component. To create a branch from a start point, pass -b:

git fetch origin
git worktree add -b hotfix/payment ../repo-hotfix origin/main

-B does the same but resets the branch if it already exists. --detach checks out a commit with no branch, which suits a read-only look at someone else's work:

git worktree add --detach ../repo-review origin/feature/payment

List. Run git worktree list to see the path, commit, and branch for each worktree. How to read git worktree list output explains each column and the -v reasons. locked marks a protected worktree. prunable marks a worktree whose administrative record Git can remove.

/home/you/repo 30788e1 [main]
/home/you/repo-hotfix 30788e1 [hotfix/payment] locked

Scripts should read git worktree list --porcelain, which prints stable key-value lines.

Remove. git worktree remove ../repo-hotfix deletes the directory and its admin record. Git refuses when the worktree has modified or untracked files. --force overrides that check. Removing a worktree never deletes its branch, so delete the branch as a separate step:

git worktree remove ../repo-hotfix
git branch -d hotfix/payment

Prune. If you delete a worktree directory with rm -rf, its record stays in .git/worktrees and git worktree list marks it prunable. git worktree prune removes those stale records, as shown in Git worktree prune or remove. Add --dry-run --verbose to see what it would drop first. Git also prunes on its own during git gc, after gc.worktreePruneExpire.

Lock and move. Lock a worktree on removable media or a network share. Prune then keeps its record while the drive is unplugged:

git worktree lock --reason "on external drive" ../repo-usb
git worktree unlock ../repo-usb

git worktree move ../repo-old ../repo-new relocates a linked worktree and updates both sides of its link. It cannot move the main worktree or a worktree that contains submodules. A locked worktree moves only after git worktree unlock, or with --force given twice. If you moved a directory by hand, run git worktree repair from the moved worktree to reconnect it.

Git Worktree Example​

Say you are halfway through feature/search in ~/shop, with an uncommitted file, when a bug report comes in for main. Instead of stashing, add a worktree for the fix. The output below is from Git 2.43.

$ git status --short
?? search.py
$ git worktree add -b hotfix/cart-total ../shop-hotfix main
Preparing worktree (new branch 'hotfix/cart-total')
HEAD is now at 8b2c6b7 Add cart
$ git worktree list
/home/you/shop 8b2c6b7 [feature/search]
/home/you/shop-hotfix 8b2c6b7 [hotfix/cart-total]

../shop-hotfix now holds a clean checkout of main on a new branch. Fix the bug and commit there:

$ cd ../shop-hotfix
$ git commit -am "Round cart total"
$ git push -u origin hotfix/cart-total

Back in ~/shop, git status --short still shows ?? search.py, untouched. git branch marks the hotfix branch with +, which means another worktree has it checked out. Once the pull request is open, remove the worktree from ~/shop:

$ cd ../shop
$ git worktree remove ../shop-hotfix
$ git worktree list
/home/you/shop 8b2c6b7 [feature/search]

The branch outlives the worktree. Delete it with git branch -d hotfix/cart-total after the pull request merges. Before that, Git refuses with error: the branch 'hotfix/cart-total' is not fully merged.

Common Errors​

fatal: '<branch>' is already used by worktree at '<path>' means another worktree has that branch checked out. Switch the other worktree away or use a new branch. See Fix "branch is already used by worktree".

fatal: cannot remove a locked working tree, lock reason: <reason> comes from remove on a locked worktree. Run git worktree unlock first, or pass -f twice. Remove a locked Git worktree walks through both paths.

fatal: '<path>' contains modified or untracked files, use --force to delete it protects uncommitted work. Commit, stash, or copy what you need, then remove the worktree.

Placing worktrees inside the main checkout makes them show up as untracked files and confuses search tools. Should worktrees go inside the repo or next to it? covers the trade-offs.

Git Worktrees vs Clones​

Each worktree has its own working files, index, and HEAD. Worktrees share local branches, tags, and the object database. A separate clone has its own local branches and remotes, so fetching in one clone does not update the other.

WorktreeClone
Object databaseSharedSeparate copy
Branches and tagsSharedSeparate
Same branch in two placesRefusedAllowed
Disk costWorking files onlyWorking files plus objects

Git Worktrees vs Clones explains when the extra isolation of a clone is worth it. For reviewing one pull request, see worktree or clone to review a PR.

Git Worktrees and Submodules​

Worktrees and submodules work together, with gaps. The git worktree manual says support for submodules is incomplete and does not recommend several checkouts of a superproject. That was still the case in Git 2.56, checked December 5, 2026. Here is what happens in practice, run with Git 2.43:

  • git worktree add leaves submodules empty. The new worktree gets .gitmodules and the gitlink, but each submodule directory is empty, even with submodule.recurse=true. Run git submodule update --init --recursive inside the new worktree.
  • Each worktree clones its own submodules. That update fetches the submodule again and stores its repository under .git/worktrees/<name>/modules/. Every linked worktree adds another copy of each submodule's history.
  • Move and remove refuse once submodules are checked out. Both fail with fatal: working trees containing submodules cannot be moved or removed. git worktree remove --force deletes the worktree and its submodule clones. Commit and push submodule work first.
  • Each worktree pins its own submodule commit. The gitlink is part of the branch, so two worktrees can sit on different submodule commits without affecting each other.
git worktree add -b feature/api ../repo-api origin/main
git -C ../repo-api submodule update --init --recursive

Coding agents hit the empty-directory case first, as a build that cannot find files under the submodule path. Add the update step to whatever script creates the worktree. What are Git Submodules? covers gitlinks and detached HEAD inside a submodule.

Worktrees with Coding Agents​

A coding agent edits files and runs commands in its current directory. Two agents in one checkout overwrite each other's edits and break each other's builds. Give each agent its own worktree on its own branch, and each one gets a private set of files and an index of its own.

Claude Code can start a session in a new worktree with the --worktree flag. You can also create the worktree yourself and start any agent from inside it:

git worktree add -b agent/auth-timeout ../repo-auth-timeout origin/main
cd ../repo-auth-timeout && claude

Shared repository state still needs care. Every agent sees the same branches, so pick unique branch names. Each worktree also needs its own node_modules, build output, and env files. Run coding agents in Git worktrees covers agent setup and cleanup in detail, and running parallel AI coding agents covers coordinating several at once. To compare tools that create and clean up worktrees for you, see the best Git worktree managers.

Treq uses its own managed workspaces instead of Git worktrees. Each workspace has its own working copy under .treq/workspaces, and Treq rebases workspace branches when their targets move.

Git Worktrees and Jujutsu Workspaces​

Jujutsu workspaces also attach several working directories to one repository. They track state differently. Git gives each worktree an index and a HEAD. Jujutsu stores each workspace's working copy as a commit that updates as files change.

Definition

Jujutsu Workspace

A working directory registered with a Jujutsu repository, with its working-copy state represented by a workspace-specific commit.

jj workspace add ../repo-hotfix
jj workspace list
jj workspace forget repo-hotfix

jj workspace forget drops the workspace record but leaves its directory on disk. Use Git worktrees in a Git workflow and Jujutsu workspaces in a Jujutsu workflow. Colocated repositories can show commits to both tools, but mixing their workspace commands without knowing what state is shared invites confusion.

Engineering Considerations​

Use worktrees when you want several checkouts inside one trust boundary. They fit reviews, hotfixes, long test runs, and agent tasks running side by side.

Worktrees are not separate repositories. A branch made in one shows up in the others, and a fetch updates the shared remote-tracking refs. Hooks and config apply across the whole repository, though Git lets you set some values per worktree through extensions.worktreeConfig.

Dependencies and ignored files are not shared. In many projects those files take up more disk than Git's object database.

Scaling and Operations​

Name worktrees after a task or pull request ID so the names stay stable:

../repo-pr-1842
../repo-task-auth-timeout

Automation has to cope with half-created worktrees and failed cleanup. Check that a branch is not already checked out before you assign it, and run git worktree prune after cleanup. Do not run repository maintenance while other Git processes are writing shared state.

Use Git submodules when one repository must version another repository in-tree. Use an OS sandbox when tasks need a real security boundary.

Next Steps​