agentleFS
Arnav-0Arnav-0/claude-code-setup/skills/git-worktree-manager/SKILL.md

git-worktree-manager

Skill0 starsChanged 7 months agoMITView on GitHub

Git Worktree Manager

  • Reads credentials
  • Deletes or force-pushes

We split this skill into the rules it gives an agent

26 rules. 21 are common, 0 are distinctive, and 5 are very specific to this project.

  • 21

    Common

    Most of the 5 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Common:

    Use this skill to run parallel feature work safely with Git worktrees.

    line 14
  2. Common:

    Create worktrees from new or existing branches with deterministic naming

    line 20
  3. Common:

    Run the manager script (creates branch if missing).

    line 40
  4. Common:

    Start app using allocated ports.

    line 42
  5. Common:

    If you use JSON automation input:

    line 54
  6. Common:

    Remove only merged + clean worktrees, or force explicitly.

    line 76
  7. Very specific:

    Use per-worktree override files mapped from allocated ports.

    line 85
  8. Very specific:

    Default strategy is base + (index * stride) with collision checks:

    line 91
  9. Common:

    Keep worktrees short-lived.

    line 127
  10. Very specific:

    Use a deterministic naming pattern (wt-<topic>).

    line 128
  11. Common:

    Run cleanup scan weekly in active repos.

    line 130
  12. Very specific:

    Use --format json for machine flows and --format text for human review.

    line 131
  13. Common:

    Never force-remove dirty worktrees unless changes are intentionally discarded.

    line 132
  14. Common:

    Before claiming setup complete:

    line 136
  15. Common:

    Use this quick selector before creating a new worktree:

    line 152
  16. Common:

    Verify git status branch matches expected branch.

    line 170
  17. Common:

    Verify app boots on allocated app port.

    line 172
  18. Common:

    Verify DB and cache endpoints target isolated ports.

    line 173
  19. Common:

    Verify branch has upstream and is merged when intended.

    line 177
  20. Common:

    Verify no uncommitted files remain.

    line 178
  21. Common:

    Verify no running containers/processes depend on this worktree path.

    line 179
  22. Very specific:

    Use worktree path naming that maps to task ID (wt-1234-auth).

    line 183
  23. Common:

    If git worktree add fails due to existing path: inspect path, do not overwrite.

    line 190
  24. Common:

    If dependency install fails: keep worktree created, mark status and continue manual recovery.

    line 191
  25. Common:

    If env copy fails: continue with warning and explicit missing file list.

    line 192
  26. Common:

    If port allocation collides with external service: rerun with adjusted base ports.

    line 193

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 -rf operations

Key Workflows

1. Create a Fully-Prepared Worktree

  1. Pick a branch name and worktree name.
  2. Run the manager script (creates branch if missing).
  3. Review generated port map.
  4. 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

  1. Scan all worktrees and stale age.
  2. Inspect dirty trees and branch merge status.
  3. 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

  1. Creating worktrees inside the main repo directory
  2. Reusing localhost:3000 across all branches
  3. Sharing one database URL across isolated feature branches
  4. Removing a worktree with uncommitted changes
  5. Forgetting to prune old metadata after branch deletion
  6. Assuming merged status without checking against the target branch

Best Practices

  1. One branch per worktree, one agent per worktree.
  2. Keep worktrees short-lived; remove after merge.
  3. Use a deterministic naming pattern (wt-<topic>).
  4. Persist port mappings in file, not memory or terminal notes.
  5. Run cleanup scan weekly in active repos.
  6. Use --format json for machine flows and --format text for human review.
  7. Never force-remove dirty worktrees unless changes are intentionally discarded.

Validation Checklist

Before claiming setup complete:

  1. git worktree list shows expected path + branch.
  2. .worktree-ports.json exists and contains unique ports.
  3. .env files copied successfully (if present in source repo).
  4. Dependency install command exits with code 0 (if enabled).
  5. 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

  1. Confirm main repo has clean baseline or intentional WIP commits.
  2. Confirm target branch naming convention.
  3. Confirm required base branch exists (main/develop).
  4. Confirm no reserved local ports are already occupied by non-repo services.

After Creation

  1. Verify git status branch matches expected branch.
  2. Verify .worktree-ports.json exists.
  3. Verify app boots on allocated app port.
  4. Verify DB and cache endpoints target isolated ports.

Before Removal

  1. Verify branch has upstream and is merged when intended.
  2. Verify no uncommitted files remain.
  3. 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 add fails 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.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.