Sub topic 4
One branch per worktree, and detached HEAD
This chapter assumes you know commit, branch, merge and diff, and that you have run git worktree add once. It covers the rule that limits which branches a worktree can use, and the detached HEAD option that works around it.
The one-branch rule
Git allows a branch to be checked out in only one worktree at a time. If two worktrees could both have main checked out, a commit made in one would move the branch under the other. That worktree's files and index would then no longer match its branch. Git refuses this setup before it can happen.
For parallel agents, the rule works in your favor. Each agent gets its own branch, so each agent's commits go to a separate line of history.
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listThe -b flag creates a new branch, agent/auth, starting at main. The branch is checked out in the new directory. main itself stays checked out in your original directory.
The common error
The usual mistake is to ask for a branch that is already checked out somewhere else.
git worktree add ../myrepo-main2 mainIf main is checked out in another worktree, Git stops with a message like fatal: 'main' is already checked out at '/path/to/myrepo'. The fix is to name a new branch with -b, or to use a detached worktree.
Detached HEAD worktrees
A detached HEAD means HEAD points straight at a commit instead of at a branch name. No branch moves when you commit, so no branch is claimed. This is why Git allows several detached worktrees at the same commit.
git worktree add --detach ../myrepo-scratch mainThis creates ../myrepo-scratch with the files from the tip of main, and no branch is checked out there.
Detached worktrees suit work you may throw away:
- Running the test suite against the current
main. - Letting an agent explore an idea without creating a branch.
- Reproducing a bug at a specific commit.
Commits made on a detached HEAD belong to no branch. If you remove the worktree without saving them, they become unreachable and Git eventually deletes them. To keep the work, create a branch while you are still in the worktree.
cd ../myrepo-scratch
git switch -c agent/experimentChoosing between the two
For agents that produce changes you plan to review and merge, use a named branch per agent. The agent/ prefix keeps these branches grouped and easy to list with git branch --list 'agent/*'.
Exercise
You run git worktree add ../myrepo-b main while main is checked out in your original directory, and Git refuses. Give two commands that would give you a second worktree starting from main, and say which one lets you commit work that you can merge later.
Show answer
Use git worktree add -b agent/b ../myrepo-b main to create a new branch, or git worktree add --detach ../myrepo-b main for a detached HEAD. The -b version lets you commit work on a named branch that you can merge later. Commits on a detached HEAD belong to no branch, so you would need git switch -c <name> to keep them.