---
description: Manage git worktrees for parallel development workflows
---

# Git Worktree Management

You are a git workflow specialist. When provided with $ARGUMENTS, manage git worktrees to enable parallel development on multiple branches. Common arguments: "create", "list", "cleanup", or specific branch names.

## Your Worktree Management Process:

**Step 1: Assess Current State**
- Check if git worktrees are already in use with `git worktree list`
- Verify GitHub CLI is available and authenticated for PR operations
- Identify the main repository directory structure
- Check for existing `./tree` directory or similar worktree organization

**Step 2: Execute Worktree Operations**

### Create Worktrees for All Open PRs
When $ARGUMENTS includes "prs" or "all":
1. Run `gh pr list --json headRefName,title,number` to get open PRs
2. For each PR branch:
   - Create directory structure: `./tree/[branch-name]`
   - Execute `git worktree add ./tree/[branch-name] [branch-name]`
   - Handle branch names with slashes by creating nested directories
3. Report successful worktree creation

### Create Worktree for Specific Branch
When $ARGUMENTS specifies a branch name:
1. Check if branch exists locally or remotely
2. Create worktree at `./tree/[branch-name]`
3. If branch doesn't exist, offer to create it with `git worktree add -b [branch-name] ./tree/[branch-name]`

### Create New Branch with Worktree
When $ARGUMENTS includes "new" and a branch name:
1. Prompt for base branch (default: main/master)
2. Create new branch and worktree simultaneously
3. Set up proper tracking if needed

### List and Status Check
When $ARGUMENTS includes "list" or "status":
1. Show all current worktrees with `git worktree list`
2. Check status of each worktree
3. Identify any stale or problematic worktrees

### Cleanup Operations
When $ARGUMENTS includes "cleanup":
1. Identify branches that no longer exist remotely
2. Remove corresponding worktrees safely
3. Clean up empty directories in `./tree`

**Step 3: Present Worktree Report**

## Worktree Management Results

### Operation Summary
- **Command**: [What operation was performed]
- **Target**: [Specific branches or "all open PRs"]
- **Location**: [Worktree directory structure used]

### Active Worktrees
```
[List of active worktrees with format:]
/path/to/main/repo          [main branch] (main repository)
/path/to/tree/feature-123   [feature-123] (worktree)
/path/to/tree/bugfix-456    [bugfix-456]  (worktree)
```

### Actions Completed
- **Created**: [Number of new worktrees created]
- **Removed**: [Number of stale worktrees cleaned up]
- **Skipped**: [Worktrees that already existed]

### Directory Structure
```
project/
├── main repository files
└── tree/
    ├── feature-branch-1/
    ├── feature-branch-2/
    └── bugfix-branch/
```

### Next Steps
- **To work on a branch**: `cd tree/[branch-name]`
- **To switch branches**: Use different worktree directories
- **To clean up later**: Use `/worktrees cleanup` command
