Git worktrees
Goal
Give each branch or pull request its own checked-out directory under one shared root, so parallel work never disturbs the primary checkout.
Input
Run inside a git repository. Supply a branch name or a PR number:
--root DIRoverrides the worktree root; default~/git/worktrees, also configurable withGIT_WORKTREE_ROOT.--base REFcreates a missing branch fromREF; without it a missing branch is an error.--pr Nfetches pull requestNfromorigininto branchpr/N.--dry-runprints the plan without mutating anything.
Output
~/git/worktrees/<project-slug>-<pr-or-branch-name>
<project-slug> comes from the repository directory name; separators in
branch names become -. Branch feature/foo in repository my-app
lands at ~/git/worktrees/my-app-feature-foo; PR 12 lands at
~/git/worktrees/my-app-pr-12. Creating prints the new path.
Workflow
Invoke scripts/worktree.py from this skill with python3:
python3 scripts/worktree.py add feature/foo --base main
python3 scripts/worktree.py add --pr 123
python3 scripts/worktree.py path feature/foo
python3 scripts/worktree.py list
python3 scripts/worktree.py remove my-app-feature-foo
python3 scripts/worktree.py prune
remove accepts a worktree directory name (looked up under the root),
a relative path, or an absolute path.
Rules
- Only create or delete directories under the configured worktree root.
- Fail closed: never overwrite an existing directory, never delete
branches, and never guess between reusing an existing branch and
creating a new one;
--basemust say so explicitly. - Never create a second worktree for a branch already checked out anywhere, and never remove the main working tree.
git worktree removerefuses dirty or locked worktrees; pass--forceonly when the operator confirmed discarding the changes.- Use the printed path as the single source of truth; never hand-build worktree directories outside this layout.
- When work ends,
removethe worktree andprune; keep or delete branches by explicit choice, never as a side effect.
Verification
git worktree list
git -C <worktree-path> status
The new path must appear registered and its status clean. For this
skills repository, also run bash scripts/validate-skills,
bash scripts/validate-support-scripts, and
python3 -m unittest tests/test_git_worktrees.py -v.