import DefinitionCard from "@site/src/components/DefinitionCard";

# 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.

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](/learn/answers/worktrees/worktrees-in-vscode) and [Zed worktrees](/learn/answers/worktrees/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/` in the main repository.

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.

| Command | What it does |
| --- | --- |
| `git worktree add  []` | Create a linked worktree at `` |
| `git worktree add -b   []` | Create a new branch and check it out in a new worktree |
| `git worktree list` | Show every worktree with its commit and branch |
| `git worktree remove ` | Delete a clean linked worktree and its admin record |
| `git worktree prune` | Drop records for worktrees whose directory is gone |
| `git worktree lock ` | Protect a worktree from `prune`, `move`, and `remove` |
| `git worktree unlock ` | Lift the lock |
| `git worktree move  ` | 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](/learn/answers/worktrees/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`:

```bash
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:

```bash
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](/learn/answers/worktrees/git-worktree-list) explains each column and the `-v` reasons. `locked` marks a protected worktree. `prunable` marks a worktree whose administrative record Git can remove.

```text
/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:

```bash
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](/learn/answers/worktrees/prune-or-delete-worktree). 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:

```bash
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.

```text
$ 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:

```text
$ 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`:

```text
$ 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: '' is already used by worktree at ''` 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"](/learn/answers/worktrees/branch-already-used-by-worktree).

`fatal: cannot remove a locked working tree, lock reason: ` comes from `remove` on a locked worktree. Run `git worktree unlock` first, or pass `-f` twice. [Remove a locked Git worktree](/learn/answers/worktrees/remove-locked-worktree) walks through both paths.

`fatal: '' 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?](/learn/answers/worktrees/nested-or-sibling-worktrees) 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.

| | Worktree | Clone |
| --- | --- | --- |
| Object database | Shared | Separate copy |
| Branches and tags | Shared | Separate |
| Same branch in two places | Refused | Allowed |
| Disk cost | Working files only | Working files plus objects |

[Git Worktrees vs Clones](./git-worktrees-vs-clones.mdx) explains when the extra isolation of a clone is worth it. For reviewing one pull request, see [worktree or clone to review a PR](/learn/answers/worktrees/worktree-or-clone-to-review-a-pr).

## Git Worktrees and Submodules

Worktrees and submodules work together, with gaps. The [`git worktree` manual](https://git-scm.com/docs/git-worktree) 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//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.

```bash
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?](./git-submodules.mdx) 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:

```bash
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](/learn/git-worktrees-for-ai) covers agent setup and cleanup in detail, and [running parallel AI coding agents](/learn/guides/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](/compare/best-git-worktree-managers).

Treq uses its own managed [workspaces](/docs/concepts/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.

```bash
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:

```text
../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](./git-submodules.mdx) when one repository must version another repository in-tree. Use an OS sandbox when tasks need a real security boundary.

## Next Steps

- [What are Stacked PRs?](./stacked-prs.mdx): organize dependent branches into reviewable changes
- [Parallel Development Workflow](/learn/workflows/git/parallel-development): apply worktrees to concurrent tasks
- [What are Coding Agents?](/learn/concepts/ai-engineering/coding-agents): what agents do inside the directory you give them
- [Run Multiple Claude Code Sessions](/learn/how-to/how-to-run-multiple-claude-code-sessions): one session per worktree
