Sub topic 3
Why a branch can be checked out only once
This chapter assumes you can commit, branch and switch in Git, and that you have created a linked worktree with git worktree add. You should also know that a branch is a named pointer to a commit. Here you will learn why Git refuses to check out one branch in two worktrees, how to read the error, and how to work around it safely.
The rule
A branch can be checked out in only one worktree at a time. This includes the main worktree. If you ask for a branch that another worktree already has, Git stops:
git worktree add ../myproject-feature feature/searchPreparing worktree (checking out 'feature/search')
fatal: 'feature/search' is already checked out at '/home/you/myproject'Newer Git versions word the last line differently: fatal: 'feature/search' is already used by worktree at '/home/you/myproject'. The meaning is the same, and the message names the path of the worktree that holds the branch.
The rule is not a limit on how many directories Git can manage. Git is protecting a relationship between a branch, an index and a working tree.
Why Git enforces it
A branch is a file-like entry, such as refs/heads/feature/search, that holds one commit ID. When you commit on a branch, Git writes the new commit ID into that entry. Branch entries live in the shared repository, so every worktree sees the same value.
Each worktree has its own HEAD, its own index and its own files on disk. The index and files are expected to match the commit that the branch points to, plus whatever changes you have made on top.
Suppose Git allowed two worktrees, A and B, on feature/search:
- Both start at commit
c1, and both working trees matchc1. - In A you edit a file and commit. The branch moves to
c2. - B's
HEADstill says "I am onfeature/search", so it now meansc2. B's index and files still matchc1. - To Git, B now looks as if you had deliberately reverted A's change. Running
git statusin B shows A's work as staged or modified, andgit commit -ain B would record the revert.
Nothing is corrupted at the file level, but the second worktree silently describes a state you never chose. Git prevents this by guaranteeing that the worktree holding a branch is the only place that moves it by committing.
How Git knows which branches are taken
Each linked worktree stores its own HEAD in a private directory under the common Git directory, at .git/worktrees/<name>/HEAD. The main worktree's HEAD is in .git/HEAD. When you try to use a branch, Git reads every worktree's HEAD and compares the branch names.
You can see the same information yourself:
git worktree list
git branchIn git branch output, a branch checked out in another worktree is marked with +, and the branch in your current worktree is marked with *:
hotfix/login-crash
* main
+ feature/searchThe marker is a quick way to check availability before you run git worktree add.
Git also treats a branch as in use when a worktree is in the middle of an operation on it. A worktree with a rebase in progress on feature/search holds that branch even though its HEAD is detached at that moment. The same applies to a bisect in progress. This is why you may see the branch reported as used when no worktree appears to be on it.
A runnable example
This script builds a small repository and triggers the error. It needs Git 2.28 or later for git init -b.
mkdir demo && cd demo
git init -b main
git config user.name "Demo User"
git config user.email "[email protected]"
git commit --allow-empty -m "first commit"
git branch feature/search
git worktree add ../demo-search feature/search
git worktree add ../demo-search-again feature/search || echo "refused, as expected"
git branchThe first worktree add succeeds. The second fails and prints the message shown earlier, pointing at ../demo-search. The final git branch lists feature/search with a +.
Now commit in the linked worktree and check that the branch moved for everyone:
git -C ../demo-search commit --allow-empty -m "work on search"
git log --oneline -1 feature/searchThe second command, run in the main worktree, shows work on search. The branch entry is shared, so the main worktree can read it. It cannot check it out.
Other operations that respect the rule
The check is not limited to git worktree add. Git applies it wherever an operation would move or remove a branch that another worktree depends on.
| Operation | What happens |
|---|---|
| `git switch feature/search` or `git checkout feature/search` in another worktree | Refused, with an "already checked out" or "already used by worktree" message |
| `git worktree add -B feature/search ../dir` | Refused, because `-B` would reset a branch that is in use |
| `git branch -d feature/search` or `-D` | Refused, with "Cannot delete branch ... checked out at ..." |
| `git branch -f feature/search <commit>` | Refused, because it would move the branch under the worktree |
| `git fetch origin feature/search:feature/search` | Refused, because it would update a checked-out branch behind its back |
These refusals all come from the same reasoning: only the owning worktree may move the branch.
Getting what you need without breaking the rule
When Git refuses, you usually want one of four things. Pick based on what you intend to do.
You want to read or test the code, not commit to it
Use a detached HEAD. A detached worktree is not on any branch, so it holds nothing and many worktrees can sit on the same commit.
git worktree add --detach ../myproject-review feature/searchThis is the right choice for code review, running tests on a colleague's branch, or building an older release. If you later decide to commit, create a branch at that point with git switch -c new-branch-name.
You want to commit, and you can use a different branch name
Create a new branch that starts at the same commit:
git worktree add -b feature/search-experiment ../myproject-experiment feature/searchThe new branch begins at the tip of feature/search. You can merge or cherry-pick later. Be aware that the two branches now diverge once either one gets a commit.
You want the branch itself in a different directory
Free the branch in the worktree that holds it, then add it elsewhere. In the worktree that currently holds it, switch away or detach:
git switch --detach
git worktree add ../myproject-feature feature/searchIf the holder is a linked worktree you no longer need, remove it instead with git worktree remove. That is covered in the cleanup chapter.
You really want the same branch twice
git worktree add --force overrides the check. A related option for git checkout is --ignore-other-worktrees.
git worktree add --force ../myproject-second feature/searchThis creates exactly the drifting situation described above. Use it only when you understand the consequences, for example to inspect a branch in a worktree you promise not to commit in.
A related case: a branch whose worktree is missing
Git also protects a branch that is assigned to a worktree whose directory has vanished, for example after you deleted it by hand. The administrative entry in .git/worktrees/ still names the branch, so Git still considers it taken. The error message mentions a path that no longer exists.
Run git worktree prune to remove entries for missing directories, then retry. If the directory is on a removable drive or network share that is only temporarily absent, the entry may be locked, and prune will leave it alone. In that case, mount the drive again, or unlock the worktree with git worktree unlock.
Common mistakes
- Reading the error as a failure of the command syntax. The message is accurate: the branch is in use, and the path tells you where.
- Using
--forceto get past the error, then committing in both places. Use--detachinstead. - Forgetting that the original clone directory is also a worktree. If you are on
mainin your normal clone,git worktree add ../x mainis refused for that reason. Create a branch, or switch the main directory to another branch first. - Deleting a worktree directory with
rm -rfand then being unable to reuse its branch. Usegit worktree remove, or rungit worktree pruneafterward.
Exercise
You are on main in your normal clone. You run git worktree add ../myproject-main main and Git refuses. Give two different commands that let you have a second directory showing the same code as main, and say what you can and cannot do in each.
Show answer
Option 1: git worktree add --detach ../myproject-main main creates a detached worktree at the current tip of main. You can build, test and read, but commits there are not on any branch until you create one with git switch -c. Option 2: git worktree add -b main-copy ../myproject-main main creates a new branch main-copy starting at the same commit. You can commit freely on main-copy, but those commits do not move main until you merge them.
Exercise
Worktree A is on feature/search and worktree B was forced onto the same branch with git worktree add --force. You commit in A. What does git status in B show, and why?
Show answer
B shows changes that look like a reversion of A's commit. B's HEAD points to the shared branch, which now refers to A's new commit. B's index and files still match the old commit, so the difference between them and the branch tip is reported as staged or modified changes. Committing in B would record that reversion.
Exercise
git worktree add ../x old-branch fails and names /home/you/old-worktree, a directory you deleted last week with rm -rf. What do you run, and why does it fix the problem?
Show answer
Run git worktree prune. The repository still has an administrative entry in .git/worktrees/ that records old-branch as checked out at that path. Prune removes entries whose directories no longer exist, which releases the branch so git worktree add can use it. If the entry was locked, run git worktree unlock on it first.