Git worktree manager
Run parallel feature work safely with Git worktrees.
How to use it
Claude Code
- Run the line below. It pulls the whole folder into
~/.claude/skills/git-worktree-manager, including the files SKILL.md points to. - Describe your job in plain words. Claude Code follows the skill from there.
npx degit alirezarezvani/claude-skills/engineering/skills/git-worktree-manager#main ~/.claude/skills/git-worktree-managerFor one project only, change the path to .claude/skills/git-worktree-manager. This skill also uses config.json, docker-compose-patterns.md, port-allocation-strategy.md — copying SKILL.md alone won't be enough. See the folder on GitHub.
Claude (web or desktop app)
- On this page open ⋯ → Download .md.
- Save it as SKILL.md in a folder, zip the folder, then Customize → Skills → + → Create skill → Upload a skill.
- Pick the file and Save. Claude shows the name and description and runs a security scan.
- Check the skill is switched on.
- Start a new chat and describe your job in plain words. The AI follows the skill from there.
ChatGPT or another app
- ChatGPT: make a Project and paste it into Instructions.
- Neither? Paste it at the top of a new chat — it works for that chat.
Not working?
- Check which app you pasted it into — the steps above name the right one.
- Some skills need the paid tier of Claude or ChatGPT.
Paste into Claude, ChatGPT or Cursor.
Source of Git worktree manager
Show the full text194 lines
| name | description |
|---|---|
| git-worktree-manager | Run parallel feature work safely with Git worktrees. Standardizes branch isolation, port allocation, environment sync, and cleanup so each worktree behaves like an independent local app. Optimized for multi-agent workflows where each agent or terminal session owns one worktree. Use when running multiple feature branches simultaneously, isolating experimental work, or coordinating multi-agent development across the same repo. |
Git Worktree Manager
Tier: POWERFUL
Category: Engineering
Domain: Parallel Development & Branch Isolation
Overview
Use this skill to run parallel feature work safely with Git worktrees. It standardizes branch isolation, port allocation, environment sync, and cleanup so each worktree behaves like an independent local app without stepping on another branch.
This skill is optimized for multi-agent workflows where each agent or terminal session owns one worktree.
Core Capabilities
- Create worktrees from new or existing branches with deterministic naming
- Auto-allocate non-conflicting ports per worktree and persist assignments
- Copy local environment files (
.env*) from main repo to new worktree - Optionally install dependencies based on lockfile detection
- Detect stale worktrees and uncommitted changes before cleanup
- Identify merged branches and safely remove outdated worktrees
When to Use
- You need 2+ concurrent branches open locally
- You want isolated dev servers for feature, hotfix, and PR validation
- You are working with multiple agents that must not share a branch
- Your current branch is blocked but you need to ship a quick fix now
- You want repeatable cleanup instead of ad-hoc
rm -rfoperations
Key Workflows
1. Create a Fully-Prepared Worktree
- Pick a branch name and worktree name.
- Run the manager script (creates branch if missing).
- Review generated port map.
- Start app using allocated ports.
python scripts/worktree_manager.py \
--repo . \
--branch feature/new-auth \
--name wt-auth \
--base-branch main \
--install-deps \
--format text
If you use JSON automation input:
cat config.json | python scripts/worktree_manager.py --format json
# or
python scripts/worktree_manager.py --input config.json --format json
2. Run Parallel Sessions
Recommended convention:
- Main repo: integration branch (
main/develop) on default port - Worktree A: feature branch + offset ports
- Worktree B: hotfix branch + next offset
Each worktree contains .worktree-ports.json with assigned ports.
3. Cleanup with Safety Checks
- Scan all worktrees and stale age.
- Inspect dirty trees and branch merge status.
- Remove only merged + clean worktrees, or force explicitly.
python scripts/worktree_cleanup.py --repo . --stale-days 14 --format text
python scripts/worktree_cleanup.py --repo . --remove-merged --format text
4. Docker Compose Pattern
Use per-worktree override files mapped from allocated ports. The script outputs a deterministic port map; apply it to docker-compose.worktree.yml.
See docker-compose-patterns.md for concrete templates.
5. Port Allocation Strategy
Default strategy is base + (index * stride) with collision checks:
- App:
3000 - Postgres:
5432 - Redis:
6379 - Stride:
10
See port-allocation-strategy.md for the full strategy and edge cases.
Script Interfaces
python scripts/worktree_manager.py --help- Create/list worktrees
- Allocate/persist ports
- Copy
.env*files - Optional dependency installation
python scripts/worktree_cleanup.py --help- Stale detection by age
- Dirty-state detection
- Merged-branch detection
- Optional safe removal
Both tools support stdin JSON and --input file mode for automation pipelines.
Common Pitfalls
- Creating worktrees inside the main repo directory
- Reusing
localhost:3000across all branches - Sharing one database URL across isolated feature branches
- Removing a worktree with uncommitted changes
- Forgetting to prune old metadata after branch deletion
- Assuming merged status without checking against the target branch
Best Practices
- One branch per worktree, one agent per worktree.
- Keep worktrees short-lived; remove after merge.
- Use a deterministic naming pattern (
wt-<topic>). - Persist port mappings in file, not memory or terminal notes.
- Run cleanup scan weekly in active repos.
- Use
--format jsonfor machine flows and--format textfor human review. - Never force-remove dirty worktrees unless changes are intentionally discarded.
Validation Checklist
Before claiming setup complete:
git worktree listshows expected path + branch..worktree-ports.jsonexists and contains unique ports..envfiles copied successfully (if present in source repo).- Dependency install command exits with code
0(if enabled). - Cleanup scan reports no unintended stale dirty trees.
References
- port-allocation-strategy.md
- docker-compose-patterns.md
- README.md for quick start and installation details
Decision Matrix
Use this quick selector before creating a new worktree:
- Need isolated dependencies and server ports -> create a new worktree
- Need only a quick local diff review -> stay on current tree
- Need hotfix while feature branch is dirty -> create dedicated hotfix worktree
- Need ephemeral reproduction branch for bug triage -> create temporary worktree and cleanup same day
Operational Checklist
Before Creation
- Confirm main repo has clean baseline or intentional WIP commits.
- Confirm target branch naming convention.
- Confirm required base branch exists (
main/develop). - Confirm no reserved local ports are already occupied by non-repo services.
After Creation
- Verify
git statusbranch matches expected branch. - Verify
.worktree-ports.jsonexists. - Verify app boots on allocated app port.
- Verify DB and cache endpoints target isolated ports.
Before Removal
- Verify branch has upstream and is merged when intended.
- Verify no uncommitted files remain.
- Verify no running containers/processes depend on this worktree path.
CI and Team Integration
- Use worktree path naming that maps to task ID (
wt-1234-auth). - Include the worktree path in terminal title to avoid wrong-window commits.
- In automated setups, persist creation metadata in CI artifacts/logs.
- Trigger cleanup report in scheduled jobs and post summary to team channel.
Failure Recovery
- If
git worktree addfails due to existing path: inspect path, do not overwrite. - If dependency install fails: keep worktree created, mark status and continue manual recovery.
- If env copy fails: continue with warning and explicit missing file list.
- If port allocation collides with external service: rerun with adjusted base ports.
| 1 | |
| 2 | name "git-worktree-manager" |
| 3 | description "Run parallel feature work safely with Git worktrees. Standardizes branch isolation, port allocation, environment sync, and cleanup so each worktree behaves like an independent local app. Optimized for multi-agent workflows where each agent or terminal session owns one worktree. Use when running multiple feature branches simultaneously, isolating experimental work, or coordinating multi-agent development across the same repo." |
| 4 | |
| 5 | |
| 6 | # Git Worktree Manager |
| 7 | |
| 8 | **Tier:** POWERFUL |
| 9 | **Category:** Engineering |
| 10 | **Domain:** Parallel Development & Branch Isolation |
| 11 | |
| 12 | ## Overview |
| 13 | |
| 14 | Use this skill to run parallel feature work safely with Git worktrees. It standardizes branch isolation, port allocation, environment sync, and cleanup so each worktree behaves like an independent local app without stepping on another branch. |
| 15 | |
| 16 | This skill is optimized for multi-agent workflows where each agent or terminal session owns one worktree. |
| 17 | |
| 18 | ## Core Capabilities |
| 19 | |
| 20 | Create worktrees from new or existing branches with deterministic naming |
| 21 | Auto-allocate non-conflicting ports per worktree and persist assignments |
| 22 | Copy local environment files (`.env*`) from main repo to new worktree |
| 23 | Optionally install dependencies based on lockfile detection |
| 24 | Detect stale worktrees and uncommitted changes before cleanup |
| 25 | Identify merged branches and safely remove outdated worktrees |
| 26 | |
| 27 | ## When to Use |
| 28 | |
| 29 | You need 2+ concurrent branches open locally |
| 30 | You want isolated dev servers for feature, hotfix, and PR validation |
| 31 | You are working with multiple agents that must not share a branch |
| 32 | Your current branch is blocked but you need to ship a quick fix now |
| 33 | You want repeatable cleanup instead of ad-hoc `rm -rf` operations |
| 34 | |
| 35 | ## Key Workflows |
| 36 | |
| 37 | ### 1. Create a Fully-Prepared Worktree |
| 38 | |
| 39 | Pick a branch name and worktree name. |
| 40 | Run the manager script (creates branch if missing). |
| 41 | Review generated port map. |
| 42 | Start app using allocated ports. |
| 43 | |
| 44 | |
| 45 | python scripts/worktree_manager.py \ |
| 46 | --repo . \ |
| 47 | --branch feature/new-auth \ |
| 48 | --name wt-auth \ |
| 49 | --base-branch main \ |
| 50 | --install-deps \ |
| 51 | --format text |
| 52 | |
| 53 | |
| 54 | If you use JSON automation input: |
| 55 | |
| 56 | |
| 57 | cat config.json | python scripts/worktree_manager.py --format json |
| 58 | # or |
| 59 | python scripts/worktree_manager.py --input config.json --format json |
| 60 | |
| 61 | |
| 62 | ### 2. Run Parallel Sessions |
| 63 | |
| 64 | Recommended convention: |
| 65 | |
| 66 | Main repo: integration branch (`main`/`develop`) on default port |
| 67 | Worktree A: feature branch + offset ports |
| 68 | Worktree B: hotfix branch + next offset |
| 69 | |
| 70 | Each worktree contains `.worktree-ports.json` with assigned ports. |
| 71 | |
| 72 | ### 3. Cleanup with Safety Checks |
| 73 | |
| 74 | Scan all worktrees and stale age. |
| 75 | Inspect dirty trees and branch merge status. |
| 76 | Remove only merged + clean worktrees, or force explicitly. |
| 77 | |
| 78 | |
| 79 | python scripts/worktree_cleanup.py --repo . --stale-days 14 --format text |
| 80 | python scripts/worktree_cleanup.py --repo . --remove-merged --format text |
| 81 | |
| 82 | |
| 83 | ### 4. Docker Compose Pattern |
| 84 | |
| 85 | Use per-worktree override files mapped from allocated ports. The script outputs a deterministic port map; apply it to `docker-compose.worktree.yml`. |
| 86 | |
| 87 | See [docker-compose-patterns.md] for concrete templates. |
| 88 | |
| 89 | ### 5. Port Allocation Strategy |
| 90 | |
| 91 | Default strategy is `base + (index * stride)` with collision checks: |
| 92 | |
| 93 | App: `3000` |
| 94 | Postgres: `5432` |
| 95 | Redis: `6379` |
| 96 | Stride: `10` |
| 97 | |
| 98 | See [port-allocation-strategy.md] for the full strategy and edge cases. |
| 99 | |
| 100 | ## Script Interfaces |
| 101 | |
| 102 | `python scripts/worktree_manager.py --help` |
| 103 | Create/list worktrees |
| 104 | Allocate/persist ports |
| 105 | Copy `.env*` files |
| 106 | Optional dependency installation |
| 107 | `python scripts/worktree_cleanup.py --help` |
| 108 | Stale detection by age |
| 109 | Dirty-state detection |
| 110 | Merged-branch detection |
| 111 | Optional safe removal |
| 112 | |
| 113 | Both tools support stdin JSON and `--input` file mode for automation pipelines. |
| 114 | |
| 115 | ## Common Pitfalls |
| 116 | |
| 117 | Creating worktrees inside the main repo directory |
| 118 | Reusing `localhost:3000` across all branches |
| 119 | Sharing one database URL across isolated feature branches |
| 120 | Removing a worktree with uncommitted changes |
| 121 | Forgetting to prune old metadata after branch deletion |
| 122 | Assuming merged status without checking against the target branch |
| 123 | |
| 124 | ## Best Practices |
| 125 | |
| 126 | One branch per worktree, one agent per worktree. |
| 127 | Keep worktrees short-lived; remove after merge. |
| 128 | Use a deterministic naming pattern (`wt-<topic>`). |
| 129 | Persist port mappings in file, not memory or terminal notes. |
| 130 | Run cleanup scan weekly in active repos. |
| 131 | Use `--format json` for machine flows and `--format text` for human review. |
| 132 | Never force-remove dirty worktrees unless changes are intentionally discarded. |
| 133 | |
| 134 | ## Validation Checklist |
| 135 | |
| 136 | Before claiming setup complete: |
| 137 | |
| 138 | `git worktree list` shows expected path + branch. |
| 139 | `.worktree-ports.json` exists and contains unique ports. |
| 140 | `.env` files copied successfully (if present in source repo). |
| 141 | Dependency install command exits with code `0` (if enabled). |
| 142 | Cleanup scan reports no unintended stale dirty trees. |
| 143 | |
| 144 | ## References |
| 145 | |
| 146 | [port-allocation-strategy.md] |
| 147 | [docker-compose-patterns.md] |
| 148 | [README.md] for quick start and installation details |
| 149 | |
| 150 | ## Decision Matrix |
| 151 | |
| 152 | Use this quick selector before creating a new worktree: |
| 153 | |
| 154 | Need isolated dependencies and server ports -> create a new worktree |
| 155 | Need only a quick local diff review -> stay on current tree |
| 156 | Need hotfix while feature branch is dirty -> create dedicated hotfix worktree |
| 157 | Need ephemeral reproduction branch for bug triage -> create temporary worktree and cleanup same day |
| 158 | |
| 159 | ## Operational Checklist |
| 160 | |
| 161 | ### Before Creation |
| 162 | |
| 163 | Confirm main repo has clean baseline or intentional WIP commits. |
| 164 | Confirm target branch naming convention. |
| 165 | Confirm required base branch exists (`main`/`develop`). |
| 166 | Confirm no reserved local ports are already occupied by non-repo services. |
| 167 | |
| 168 | ### After Creation |
| 169 | |
| 170 | Verify `git status` branch matches expected branch. |
| 171 | Verify `.worktree-ports.json` exists. |
| 172 | Verify app boots on allocated app port. |
| 173 | Verify DB and cache endpoints target isolated ports. |
| 174 | |
| 175 | ### Before Removal |
| 176 | |
| 177 | Verify branch has upstream and is merged when intended. |
| 178 | Verify no uncommitted files remain. |
| 179 | Verify no running containers/processes depend on this worktree path. |
| 180 | |
| 181 | ## CI and Team Integration |
| 182 | |
| 183 | Use worktree path naming that maps to task ID (`wt-1234-auth`). |
| 184 | Include the worktree path in terminal title to avoid wrong-window commits. |
| 185 | In automated setups, persist creation metadata in CI artifacts/logs. |
| 186 | Trigger cleanup report in scheduled jobs and post summary to team channel. |
| 187 | |
| 188 | ## Failure Recovery |
| 189 | |
| 190 | If `git worktree add` fails due to existing path: inspect path, do not overwrite. |
| 191 | If dependency install fails: keep worktree created, mark status and continue manual recovery. |
| 192 | If env copy fails: continue with warning and explicit missing file list. |
| 193 | If port allocation collides with external service: rerun with adjusted base ports. |
| 194 |
Discussion
Browse more free Claude skills or everything in Development.