Sub topic 3
Why a branch can be checked out only once
This chapter assumes you can commit, create branches and switch between them. It also assumes you know the difference between the working directory, the index and the repository history, and that you have already created a linked worktree with git worktree add.
The rule
Git allows a branch to be checked out in only one worktree at a time. If you try to check it out a second time, Git refuses.
Suppose feature/search is checked out in your main directory. This command then fails:
git worktree add ../myproject-feature feature/searchGit prints an error similar to this:
fatal: 'feature/search' is already checked out at '/home/you/myproject'The same check applies when you run git switch or git checkout inside a linked worktree. You cannot switch to a branch that another worktree holds.
Why Git enforces it
A branch is a name that points to a commit. When you commit on a checked-out branch, Git moves that name forward. Your working directory and index are then updated to match the new commit.
Now imagine two worktrees on the same branch. A commit in worktree A moves the branch name. Worktree B still has the old files and the old index, but its branch now points to a newer commit. In worktree B, git status would report the difference between the old files and the new commit as staged and unstaged changes that you never made. A commit there could silently undo the work from worktree A.
The one-branch-per-worktree rule prevents this. Each branch has exactly one working directory that is allowed to move it.
Reading the error
The error message names the path of the worktree that holds the branch. That path is the first thing to check. You have several ways forward, depending on what you want:
| Goal | What to do |
|---|---|
| Work on the branch in the new directory | Switch the other worktree to a different branch or a detached HEAD, then run `git worktree add` again |
| Only read or review the code | Use `--detach` to check out the commit without the branch |
| Build on top of the branch | Create a new branch that starts from it with `-b` |
| You no longer need the other worktree | Remove it with `git worktree remove` |
The detached option is the one you use most often for review:
git fetch origin
git worktree add --detach ../myproject-review origin/feature/searchA detached HEAD points at a commit instead of a branch name. No branch is claimed, so the rule does not apply. You can have as many detached worktrees on the same commit as you like.
To build on a branch without claiming it, create a new branch from it:
git worktree add ../myproject-search-experiment -b search-experiment feature/searchHere search-experiment is a new branch that starts at the same commit as feature/search. The original branch stays in its current worktree. You can merge search-experiment back later.
Finding the worktree that holds a branch
If you do not remember which directory holds the branch, list your worktrees:
git worktree listEach line shows the path, the current commit and the branch name in brackets. Find the line with your branch, then either switch that worktree away from the branch or remove it.
Overriding the check
git worktree add has a --force option (-f) that skips the check. Avoid it for ordinary work. It recreates the situation described above, where two working directories disagree about one branch. Only use it if you know the other worktree will not commit to that branch.
Exercise
You run git worktree add ../review feature/search and Git reports that feature/search is already checked out. You only want to read the code and run the tests. Which command do you use instead, and why does it avoid the error?
Show answer
Run git worktree add --detach ../review feature/search. A detached worktree points at a commit and not at a branch name, so it does not claim the branch. The rule only limits how many worktrees can hold the same branch, so the new directory can check out the same commit.
Exercise
A teammate says they deleted an old worktree directory with rm -rf, but Git still says its branch is checked out there. What is happening, and how do you fix it?
Show answer
The directory is gone, but the repository still holds the worktree's administrative entry, so Git treats the branch as checked out. Run git worktree prune to delete the stale entry. After that, the branch can be checked out again.