Smart Git Worktree Management

VerifiedSafe

Manages git worktrees for a monorepo: creates worktrees from GitHub issues with automatic pnpm install, hook script copy, and branch name generation; lists active worktrees with commit and ahead/behind status; removes merged worktrees and prunes stale references. Ideal for developers juggling multiple issues simultaneously.

Sby Skills Guide Bot
DevelopmentIntermediate
306/2/2026
Claude Code
#git-worktree#issue-management#monorepo#branch-management

Recommended for

Our review

Manages Git worktrees for a monorepo: create, list, and cleanup with automatic dependency installation and GitHub issue validation.

Strengths

  • Automates worktree creation from GitHub issues with issue state validation
  • Automatically installs dependencies with pnpm after creation
  • Provides a formatted table of active worktrees with ahead/behind status
  • Cleans up worktrees whose branches have been merged into main

Limitations

  • Specific to the USOPC monorepo directory structure
  • Requires the GitHub CLI (`gh`) to be installed and configured
  • Does not handle merge conflicts during cleanup
When to use it

Use this skill to quickly create an isolated work environment for a GitHub issue, or to clean up stale worktrees after merging.

When not to use it

Do not use it if you are working outside the USOPC monorepo or if you prefer to manage worktrees manually with Git.

Security analysis

Safe
Quality score90/100

The skill only executes standard git worktree operations, pnpm install, and gh issue view, all applied locally within the repository. There is no command to fetch or execute external scripts, delete files outside controlled worktree paths, or exfiltrate data. The allowed tools are restricted to safe, legitimate development commands.

No concerns found

Examples

Create worktree for issue #42
/worktree create 42
List all active worktrees
/worktree list
Clean up merged worktrees
/worktree cleanup

name: worktree description: Smart worktree management — create, list, and clean up git worktrees with automatic gotcha handling (pnpm install, hook script copy, issue validation). argument-hint: <create|list|cleanup> [issue-number] disable-model-invocation: true allowed-tools: Bash(git *), Bash(cd *), Bash(pnpm *), Bash(gh issue view *), Bash(gh repo view *), Bash(ls *), Glob, Read

Smart Worktree Management

You are managing git worktrees for the USOPC Athlete Support Agent monorepo. The argument is: $ARGUMENTS.

Parse the argument to determine the subcommand:

  • create <issue-number> — Create a new worktree for an issue
  • list — List active worktrees with status
  • cleanup — Remove worktrees whose branches have been merged

If no subcommand is provided or the argument is empty, print the usage:

Usage: /worktree <command> [args]
  create <issue-number>  — Create worktree for a GitHub issue
  list                   — List active worktrees with branch status
  cleanup                — Remove merged worktrees and prune refs

Subcommand: create <issue-number>

Step 1: Validate the issue exists

Run gh issue view <issue-number> --json number,title,state to confirm the issue exists and is open. If the issue doesn't exist or is closed, warn the user and stop.

Step 2: Determine paths

  • Get the main repo root: git rev-parse --show-toplevel (if in a worktree, use git worktree list to find the main repo)
  • Worktree path: ../usopc-issue-<number> relative to the main repo (i.e., a sibling directory)
  • Derive a short branch name from the issue title: feat/<kebab-case-summary> (max 50 chars, lowercase, hyphens only)

Step 3: Fetch latest main

Run git fetch origin main to ensure we branch from the latest main.

Step 4: Create the worktree

git worktree add <worktree-path> -b <branch-name> origin/main

If the worktree path already exists, warn and stop.

Step 5: Install dependencies

cd <worktree-path> && pnpm install

Step 6: Print summary

Worktree created:
  Path:   <worktree-path>
  Branch: <branch-name>
  Issue:  #<number> — <title>

Dependencies installed.
Navigate with: cd <worktree-path>

Subcommand: list

Step 1: List worktrees

Run git worktree list and parse the output.

Step 2: For each worktree (excluding bare/main)

  • Show the path, branch name
  • Run git -C <path> log --oneline -1 for latest commit
  • Run git -C <path> rev-list --left-right --count origin/main...<branch> for ahead/behind status

Step 3: Print formatted table

Active worktrees:
  ../usopc-issue-42  feat/add-auth        +3 / -0  (latest: abc1234 Add auth middleware)
  ../usopc-issue-55  fix/query-perf       +1 / -2  (latest: def5678 Fix slow query)

Subcommand: cleanup

Step 1: List worktrees

Run git worktree list and identify non-main worktrees.

Step 2: Check each branch

For each worktree branch, check if it has been merged into origin/main:

git branch --merged origin/main | grep <branch-name>

Step 3: Remove merged worktrees

For each merged worktree, remove it:

git worktree remove <path>

Print which worktrees were removed.

Step 4: Prune and clean up

git worktree prune
git fetch --prune origin

Step 5: Print summary

Cleaned up:
  Removed: ../usopc-issue-42 (feat/add-auth — merged)
  Removed: ../usopc-issue-55 (fix/query-perf — merged)
  Pruned stale worktree references.

If no worktrees were eligible for cleanup, say so.

Important notes

  • Always use absolute paths when running git commands to avoid confusion between worktree and main repo.
  • The main repo root can be found via git worktree list — it's the first entry (the one without [branch] or with [main]).
  • scripts/update-hours.mjs and scripts/stamp-readme-hours.mjs are tracked in git, so they'll be available in worktrees automatically — no copying needed.
Related skills