Git Worktree Manager
Manage parallel development workflows using Git worktrees with deterministic naming, automatic port allocation, environment file synchronization, dependency installation, and cleanup automation. Optimized for multi-agent workflows where each agent or terminal session owns an isolated worktree with its own ports, environment, and running services.
Core Capabilities
- Worktree lifecycle — create worktrees from new or existing branches with deterministic naming, copy
.env files, install dependencies by lockfile detection, list with clean/dirty + ahead/behind status, and safely remove with uncommitted-change detection.
- Port allocation — deterministic per-worktree assignment (
base + index * stride), collision detection against running processes, persistent map in .worktree-ports.json, and Docker Compose override generation.
- Multi-agent isolation — one branch per worktree, one agent per worktree, no shared state, conflict-free parallel execution, task-ID mapping for traceability.
- Cleanup automation — stale detection by age, merged-branch detection for safe removal, dirty-state warnings, and bulk cleanup with safety confirmations.
When to Use
- You need 2+ concurrent branches open with running dev servers.
- You want isolated environments for feature work, hotfixes, and PR review.
- Multiple AI agents need separate workspaces that do not interfere.
- Your current branch is blocked but a hotfix is urgent.
- You want automated cleanup instead of manual
rm -rf operations.
Clarify First
Before managing worktrees, confirm these inputs. If any is unknown or vague, ASK — do not assume:
Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions at the top of the artifact.
Tools
| Tool |
Purpose |
Command |
worktree_manager.py |
List, create, remove, and clean up worktrees |
python scripts/worktree_manager.py list |
port_allocator.py |
Assign/check/release deterministic port blocks; sync registry |
python scripts/port_allocator.py status |
worktree_validator.py |
Validate worktree health (stale, missing branch, env parity, port conflicts, lockfile) |
python scripts/worktree_validator.py --stale-days 14 |
References
Load the reference that matches the task — keep this file lean and pull detail on demand:
- references/setup-and-ports.md — quick-start
git worktree commands, the deterministic port-allocation strategy and .worktree-ports.json format, collision-detection snippet, the full setup-worktree.sh script, and the Docker Compose per-worktree override. Read when creating a worktree or wiring up ports/services.
- references/cleanup-and-workflows.md — the
cleanup-worktrees.sh safe-cleanup script, the multi-agent assignment pattern and rules, the scenario→action decision matrix, and the post-creation validation checklist. Read when automating cleanup or coordinating agents.
- references/operations-playbook.md — common pitfalls, best practices, the troubleshooting table, and the success-criteria bar. Read before shipping a workflow or when diagnosing problems.
Scope & Limitations
This skill covers:
- Git worktree lifecycle: creation, listing, status inspection, and removal
- Deterministic port allocation and collision avoidance for parallel dev servers
- Environment file synchronization and Docker Compose override patterns
- Multi-agent workspace isolation strategies and cleanup automation
This skill does NOT cover:
- Git branching strategies or merge conflict resolution (see
pr-review-expert and release-manager)
- Secret rotation, vault integration, or credential management (see
env-secrets-manager)
- CI/CD pipeline configuration or automated test orchestration (see
ci-cd-pipeline-builder)
- Monorepo package management, workspace linking, or cross-package dependency resolution (see
monorepo-navigator)
Integration Points
| Skill |
Integration |
Data Flow |
env-secrets-manager |
Worktree setup copies .env files that contain secrets managed by this skill |
.env files flow from main repo to each worktree; secret references remain consistent across all copies |
ci-cd-pipeline-builder |
CI pipelines can spin up worktrees for parallel test matrix execution |
Pipeline config triggers setup-worktree.sh per matrix job; port allocation prevents service collisions |
release-manager |
Release branches get dedicated worktrees for stabilization while feature work continues |
Release worktree is created from the release branch; merged status drives cleanup automation |
monorepo-navigator |
In monorepo setups, worktrees must respect package boundaries and shared dependencies |
Worktree creation inherits the monorepo root lockfile; package-level dev servers use allocated port blocks |
pr-review-expert |
PR reviews can be performed in isolated worktrees with running code for manual validation |
Reviewer creates a worktree at the PR branch, runs the dev server on allocated ports, and removes after review |
tech-debt-tracker |
Stale worktrees and abandoned branches surface as tech debt indicators |
Cleanup script output feeds into debt tracking; worktree age and merge status inform priority scores |
1---
2name: git-worktree-manager
3description: >
4 Manage parallel development with Git worktrees: creation with port allocation, environment
5 sync, branch isolation, and cleanup. Use when working multiple branches at once, running
6 parallel CI validations, or isolating agent workspaces.
7license: MIT + Commons Clause
8metadata:
9 version: 1.1.0
10 author: borghei
11 category: engineering
12 domain: developer-tooling
13 tier: POWERFUL
14 updated: 2026-06-17
15 frameworks: git-worktree
16---
17
18# Git Worktree Manager
19
20Manage parallel development workflows using Git worktrees with deterministic naming, automatic port allocation, environment file synchronization, dependency installation, and cleanup automation. Optimized for multi-agent workflows where each agent or terminal session owns an isolated worktree with its own ports, environment, and running services.
21
22## Core Capabilities
23
24- **Worktree lifecycle** — create worktrees from new or existing branches with deterministic naming, copy `.env` files, install dependencies by lockfile detection, list with clean/dirty + ahead/behind status, and safely remove with uncommitted-change detection.
25- **Port allocation** — deterministic per-worktree assignment (`base + index * stride`), collision detection against running processes, persistent map in `.worktree-ports.json`, and Docker Compose override generation.
26- **Multi-agent isolation** — one branch per worktree, one agent per worktree, no shared state, conflict-free parallel execution, task-ID mapping for traceability.
27- **Cleanup automation** — stale detection by age, merged-branch detection for safe removal, dirty-state warnings, and bulk cleanup with safety confirmations.
28
29## When to Use
30
31- You need 2+ concurrent branches open with running dev servers.
32- You want isolated environments for feature work, hotfixes, and PR review.
33- Multiple AI agents need separate workspaces that do not interfere.
34- Your current branch is blocked but a hotfix is urgent.
35- You want automated cleanup instead of manual `rm -rf` operations.
36
37## Clarify First
38
39Before managing worktrees, confirm these inputs. If any is unknown or vague, ASK — do not assume:
40
41- [ ] **Operation** — create, list, remove, or clean up worktrees (selects the `worktree_manager.py` action vs `port_allocator.py` vs `worktree_validator.py`)
42- [ ] **Branch & base** — which branch(es) the worktree(s) track and from what base (drives worktree naming and isolation)
43- [ ] **Port/service needs** — whether dev servers or Docker need allocated ports (drives the deterministic port block and the Docker Compose override generated)
44
45Stop rule: ask only the 2-3 that most change the output. If the user says "just draft it," proceed and list your assumptions at the top of the artifact.
46
47## Tools
48
49| Tool | Purpose | Command |
50|------|---------|---------|
51| `worktree_manager.py` | List, create, remove, and clean up worktrees | `python scripts/worktree_manager.py list` |
52| `port_allocator.py` | Assign/check/release deterministic port blocks; sync registry | `python scripts/port_allocator.py status` |
53| `worktree_validator.py` | Validate worktree health (stale, missing branch, env parity, port conflicts, lockfile) | `python scripts/worktree_validator.py --stale-days 14` |
54
55## References
56
57Load the reference that matches the task — keep this file lean and pull detail on demand:
58
59- **[references/setup-and-ports.md](references/setup-and-ports.md)** — quick-start `git worktree` commands, the deterministic port-allocation strategy and `.worktree-ports.json` format, collision-detection snippet, the full `setup-worktree.sh` script, and the Docker Compose per-worktree override. Read when creating a worktree or wiring up ports/services.
60- **[references/cleanup-and-workflows.md](references/cleanup-and-workflows.md)** — the `cleanup-worktrees.sh` safe-cleanup script, the multi-agent assignment pattern and rules, the scenario→action decision matrix, and the post-creation validation checklist. Read when automating cleanup or coordinating agents.
61- **[references/operations-playbook.md](references/operations-playbook.md)** — common pitfalls, best practices, the troubleshooting table, and the success-criteria bar. Read before shipping a workflow or when diagnosing problems.
62
63## Scope & Limitations
64
65**This skill covers:**
66- Git worktree lifecycle: creation, listing, status inspection, and removal
67- Deterministic port allocation and collision avoidance for parallel dev servers
68- Environment file synchronization and Docker Compose override patterns
69- Multi-agent workspace isolation strategies and cleanup automation
70
71**This skill does NOT cover:**
72- Git branching strategies or merge conflict resolution (see `pr-review-expert` and `release-manager`)
73- Secret rotation, vault integration, or credential management (see `env-secrets-manager`)
74- CI/CD pipeline configuration or automated test orchestration (see `ci-cd-pipeline-builder`)
75- Monorepo package management, workspace linking, or cross-package dependency resolution (see `monorepo-navigator`)
76
77## Integration Points
78
79| Skill | Integration | Data Flow |
80|-------|-------------|-----------|
81| `env-secrets-manager` | Worktree setup copies `.env` files that contain secrets managed by this skill | `.env` files flow from main repo to each worktree; secret references remain consistent across all copies |
82| `ci-cd-pipeline-builder` | CI pipelines can spin up worktrees for parallel test matrix execution | Pipeline config triggers `setup-worktree.sh` per matrix job; port allocation prevents service collisions |
83| `release-manager` | Release branches get dedicated worktrees for stabilization while feature work continues | Release worktree is created from the release branch; merged status drives cleanup automation |
84| `monorepo-navigator` | In monorepo setups, worktrees must respect package boundaries and shared dependencies | Worktree creation inherits the monorepo root lockfile; package-level dev servers use allocated port blocks |
85| `pr-review-expert` | PR reviews can be performed in isolated worktrees with running code for manual validation | Reviewer creates a worktree at the PR branch, runs the dev server on allocated ports, and removes after review |
86| `tech-debt-tracker` | Stale worktrees and abandoned branches surface as tech debt indicators | Cleanup script output feeds into debt tracking; worktree age and merge status inform priority scores |