Sub topic 4
One branch per worktree, and detached HEAD
This chapter assumes you are comfortable with everyday Git (commit, branch, merge, diff). It also assumes you have created a linked worktree with git worktree add, as covered in the previous chapter. Here you learn the rule that decides which branch each worktree may use, the errors it produces, and how to use a detached HEAD without losing work.
The rule: a branch lives in one worktree at a time
Git refuses to check out a branch in a second worktree while another worktree already has it checked out. Try this in a repository where main is checked out in the main worktree:
git worktree add ../myrepo-main2 mainGit stops with an error like this:
fatal: 'main' is already checked out at '/home/you/myrepo'Git 2.42 and later word it as 'main' is already used by worktree at '...'. The meaning is the same.
Why Git enforces it
Each worktree has its own HEAD, its own index, and its own checked-out files. Branches are different. A branch is a reference stored in the shared repository, and every worktree sees the same one.
Suppose two worktrees had main checked out. A commit in the first worktree would move main forward. The second worktree's index and files would still describe the old tip. The next git status there would show the new commit's changes as if you had reverted them. A commit from that worktree could silently undo the first one. The one-branch-per-worktree rule prevents this mismatch.
For parallel agents this rule is useful. It forces every agent onto a different branch, so no two agents can move the same ref.
What you see in the main worktree
The main worktree usually holds your integration branch, often main. That branch is therefore taken. Every agent worktree needs either a new branch or a detached HEAD.
flowchart TDThe diagram below shows the decision you make for each new worktree.
Creating worktrees on new branches
The form you will use most is -b. It creates the branch and the worktree in one step:
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listThe last argument, main, is the commit the new branch starts from. It can be a branch, a tag, or a commit hash. Starting from main does not check main out in the new worktree. It only sets the first commit of agent/auth, so the rule is not triggered.
If you leave out -b and the start point, Git creates a branch for you. It names the branch after the last part of the path and bases it on the current HEAD:
git worktree add ../authThis creates a branch named auth. Prefer the explicit form. The implicit one depends on whatever you happen to have checked out.
Flags that change the branch behavior
| Flag | Effect | When to use it |
|---|---|---|
| `-b name` | Create branch `name`. Fails if it already exists. | A new agent task. |
| `-B name` | Create `name`, or reset it to the start point if it exists. | Restarting an agent task from scratch. |
| `--detach` (or `-d`) | Check out the commit with no branch. | Throwaway experiments and read-only inspection. |
| `--force` (or `-f`) | Skip the "already checked out" check. | Almost never. See the warning below. |
-B discards the previous position of the branch. If agent/auth had commits you did not merge, they become reachable only through the reflog. Check with git log main..agent/auth before you reset.
Using an existing branch
If the branch already exists and is not checked out anywhere, name it without -b:
git worktree add ../myrepo-auth agent/authThis is how you resume work. You removed a worktree earlier but kept the branch, and now you give the agent a fresh directory on the same branch.
Finding out where a branch is checked out
When you hit the "already checked out" error, find which worktree holds the branch. git worktree list shows every worktree with its branch. git branch marks branches checked out in other worktrees with a +:
git worktree list
git branch
git branch --format='%(refname:short) %(worktreepath)'In git branch output, * marks the current worktree's branch and + marks a branch checked out in a different linked worktree. The %(worktreepath) field prints the path, or nothing if the branch is free. This also works in a script that decides whether a task name is available.
The rule applies inside a worktree too. If you are in ../myrepo-auth and run git switch main, Git refuses because the main worktree has main. Switch to another branch, or detach.
Detached HEAD
Normally HEAD points to a branch name, and a new commit moves that branch. In a detached HEAD state, HEAD points straight at a commit hash. No branch moves when you commit.
git worktree add --detach ../myrepo-scratch mainThe new worktree starts at the current tip of main. git status there reports HEAD detached at <hash>. Because no branch is checked out, many detached worktrees can sit on the same commit without breaking the rule. That is what makes detached mode useful here.
Good uses of a detached worktree
- Running the test suite against
mainwhile the main worktree has uncommitted edits. - Checking out a tag or an old commit to reproduce a bug.
- Letting an agent explore an approach whose result you expect to throw away.
- Reading code on
origin/mainwithout a local branch.
The risk: commits with no name
Commits made on a detached HEAD are reachable only through HEAD. If you switch away or remove the worktree, nothing else points at them. Git treats them as unreachable and may delete them during garbage collection, by default after about two weeks.
Each worktree keeps its own HEAD reflog, and that reflog is stored with the worktree's administrative data. Removing the worktree deletes this record too, so you lose the easy way to find the lost hashes.
Before you leave a detached worktree where you want to keep the work, give it a branch:
cd ../myrepo-scratch
git switch -c agent/scratch-ideaIf you have already left, look for the hash in the terminal history or the agent's log. Then recover it:
git branch rescue <hash>If you never saw the hash, git fsck --lost-found can list unreachable commits. This works only until garbage collection runs.
Choosing between branches and detached HEAD for agents
For agents that produce work you intend to review and merge, use a named branch. The branch is the unit you diff, review, and merge, as later chapters show. A name such as agent/auth also tells you which task a branch came from.
Use detached HEAD when the result is disposable, or when you want a clean checkout of a commit that a branch already occupies.
A practical convention follows from this:
- Give the main worktree the integration branch and do not run agents there.
- Give every agent task its own
agent/<task>branch, created with-bfrom the same base commit. - Use
--detachfor verification and exploration worktrees. - Before removing any worktree, confirm that no commits exist that a branch does not contain.
Branch names and paths
Branch names may contain slashes, so agent/auth is valid. The worktree path is a directory name and should avoid slashes. This is why the examples use ../myrepo-auth for the path and agent/auth for the branch. Git does not link the two names, so keep your own naming consistent. That makes git worktree list easy to read.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `'main' is already checked out` | Another worktree has the branch. | Use `-b` for a new branch, or `--detach`. |
| `a branch named 'agent/auth' already exists` | You used `-b` on an existing branch. | Omit `-b` to reuse it, or use `-B` to reset it. |
| `git switch main` fails in a worktree | The main worktree holds `main`. | Switch to another branch, or detach. |
| Commits vanish after removing a worktree | They were made on a detached HEAD. | Create a branch first. Recover with `git branch rescue <hash>`. |
Exercise
You run git worktree add -b agent/api ../myrepo-api main and then git worktree add -b agent/api2 ../myrepo-api2 agent/api. Both succeed. Then you run git worktree add ../myrepo-api3 agent/api. What happens, and what are two ways to get a third worktree that starts from the same commit?
Show answer
The third command fails, because agent/api is already checked out in ../myrepo-api. The second command succeeded because agent/api was only a start point for the new branch agent/api2, not checked out. To start a third worktree from the same commit, create another new branch with git worktree add -b agent/api3 ../myrepo-api3 agent/api, or detach with git worktree add --detach ../myrepo-api3 agent/api.
Exercise
An agent made two commits in a worktree created with --detach. You ran git worktree remove on it, and it succeeded. Why did Git allow it, what is the danger, and how would you have avoided it?
Show answer
git worktree remove only refuses when the directory has uncommitted or untracked changes, and the commits were already committed. The two commits were reachable only from the detached HEAD, so after removal they are unreachable and garbage collection can delete them. Before removing, run git log --oneline main..HEAD in the worktree and, if it lists commits to keep, run git switch -c <name>. If it is already removed, use git branch rescue <hash> with a hash you can find, before garbage collection runs.