# How to Use git worktree add for a New or Existing Branch

_`git worktree add  ` checks out an existing branch. `git worktree add -b   ` creates the branch and the worktree together._

## The Fix

Create a new branch from `main` in a sibling directory:

```bash
git worktree add -b new-feature ../repo-new-feature main
```

Check out a branch that already exists:

```bash
git worktree add ../repo-feature feature
```

Git 2.43 prints what it did:

```text
Preparing worktree (new branch 'new-feature')
HEAD is now at 54d33c8 init
Preparing worktree (checking out 'feature')
HEAD is now at 54d33c8 init
```

Then `cd` into the path and work as usual. `git worktree list` shows every worktree and its branch:

```text
/src/repo              54d33c8 [main]
/src/repo-feature      54d33c8 [feature]
/src/repo-new-feature  54d33c8 [new-feature]
```

## Other Forms

| Command | Result |
|---|---|
| `git worktree add ../quick-fix` | New branch `quick-fix` from `HEAD`, named after the directory |
| `git worktree add ../repo-hotfix hotfix` | When only `origin/hotfix` exists, creates local `hotfix` tracking it |
| `git worktree add --track -b fix ../repo-fix origin/hotfix` | New branch `fix` that tracks `origin/hotfix` |
| `git worktree add ../repo-review origin/hotfix` | Detached `HEAD` at that commit, no branch |
| `git worktree add --detach ../repo-review HEAD` | Detached `HEAD`, for builds or read-only review |
| `git worktree add -B feature ../repo-feature main` | Creates `feature`, or resets it to `main` if it exists |

Passing a remote-tracking ref such as `origin/hotfix` gives a detached worktree. Pass the plain branch name, or use `--track -b`, when you want to commit and push from it.

## Why It Fails

`-b` refuses a name that already exists:

```text
Preparing worktree (new branch 'feature')
fatal: a branch named 'feature' already exists
```

Drop `-b` to check out the existing branch, or use `-B` to reset it. Use `-B` only when you mean to throw away that branch's commits.

A branch can be checked out in one worktree at a time. Adding a branch that another worktree holds fails with `fatal: 'feature' is already used by worktree at ''`. [Fix "branch is already used by worktree"](./branch-already-used-by-worktree.md) covers that error.

Put the path outside the repository, as in `../repo-feature`. A worktree inside the main checkout shows up as untracked files there. [Should Git worktrees go inside the repo or next to it?](./nested-or-sibling-worktrees.md) explains the trade-off.

## How Treq Handles It

Treq does not ask you to run `git worktree add`. It creates a managed [workspace](/docs/concepts/workspaces) with its own working copy and branch under `.treq/workspaces/`, then starts your shell or agent in it. `treq add` does the same from the [CLI](/docs/reference/cli).

## Related

- [Git worktree vs branch: what is the difference?](./worktree-vs-branch.md)
- [Prune or delete a Git worktree](./prune-or-delete-worktree.md)
- [What are Git Worktrees?](/learn/concepts/git/git-worktrees)
