Using Git Worktrees
Discover the layout
Run scripts/discover-worktree-layout.sh once from the initial cwd to discover the layout:
worktree: use the reportedworktree; starting there means the user already selected that checkout, so do not create another for the same target. Create another only when the task requires a different revision and no suitable checkout exists.single-container: use the sole reportedrepository.mixed-container: select the reportedrepositorythat matches the task.- Nonzero exit: the target is not a recognized Git workspace. Stop, unless the task is to set up a new container there.
Finish discovery before recursive file searches, and scope them to the selected checkout rather than the worktree container.
workspace is the shared .agents root and is always reported. container is where checkouts live and is reported for bare repositories. They differ when the container is nested, as in project/{.agents,worktrees/{.bare,main,…}}. Values are resolved from any starting depth, including inside a checkout, so use reported values rather than deriving them from cwd.
Structure
- Use or create
.agentsonce, at the reportedworkspace; never add a per-worktree copy. - For new single-repository containers, use
project/{.agents,worktrees/{.bare,main,…}}. - When
containeris reported, create additional checkouts as its children. Otherwise inspect existing worktree paths and follow their convention. - Follow existing path and branch conventions.
- Never clone another repository inside a worktree container; check for an existing checkout under the project's own paths first.
- Use the clean-git-repo skill for bulk cleanup.
Create a checkout
- Give
git -C <bare> worktree addan absolute path; a relative one resolves under the bare repository. - Create review worktrees from the pinned head SHA;
git fetch --prunecan delete manually fetched remote-tracking refs without a corresponding remote branch. - Address the new checkout explicitly (
git -C,workdir,pnpm --dir); creating a worktree or clone does not change the shell's directory.