AgentSkillsCN

git-worktree-develop

利用 Git Worktrees 与基础分支策略,开展并行的多主题开发。当用户:(1) 希望并行处理多个相关功能;(2) 提及“worktree”、“基础分支”或“并行开发”;(3) 希望通过管理会议协调子会议;(4) 提出“按主题拆分”或“多主题开发”时,可使用此技能。内容涵盖:基础分支的创建、Worktree 的初始化、主题分支的管理、PR 策略(主题 PR 合并到基础分支,根 PR 合并到主分支),以及管理者与子会议的协调。

SKILL.md
--- frontmatter
name: git-worktree-develop
description: >-
  Parallel multi-topic development using git worktrees with a base branch strategy. Use when: (1)
  User wants to work on multiple related features in parallel, (2) User mentions 'worktree', 'base
  branch', or 'parallel development', (3) User wants a manager session to coordinate child sessions,
  (4) User says 'split into topics' or 'multi-topic development'. Covers: base branch creation,
  worktree initialization, topic branch management, PR strategy (topic PRs into base, root PR into
  main), and manager/child session coordination.

Git Worktree Multi-Topic Development

Coordinate parallel development of multiple related features using git worktrees, a shared base branch, and manager/child Claude Code sessions.

Architecture

code
main (default branch)
  └── base/<project-name>  (base branch, created by manager)
        ├── <project-name>/topicA  (child branch → PR into base)
        ├── <project-name>/topicB  (child branch → PR into base)
        └── <project-name>/topicC  (child branch → PR into base)

worktrees/
  ├── <topicA>/  (worktree for topicA branch)
  ├── <topicB>/  (worktree for topicB branch)
  └── <topicC>/  (worktree for topicC branch)

Each topic gets its own worktree directory, its own branch, and its own PR targeting the base branch. The manager merges topic PRs into the base branch, then creates one root PR from base into main.

Roles

Manager Session (root repo)

The manager session operates from the repo root directory. It:

  1. Creates the base branch from main
  2. Creates worktrees for each topic
  3. Reviews and merges topic PRs into the base branch
  4. Creates the final root PR (base → main)

Child Sessions (worktree dirs)

Each child session operates from its own worktree directory. It:

  1. Implements its assigned topic
  2. Commits and pushes to its topic branch
  3. Creates a PR targeting the base branch
  4. Addresses review feedback from the manager

Manager Workflow

Step 1: Create Base Branch

bash
# Ensure main is up to date
git checkout main
git pull origin main

# Create and push the base branch
git checkout -b base/<project-name>
git push -u origin base/<project-name>

# Return to main
git checkout main

Step 2: Create Worktrees

For each topic:

bash
# Create worktree with a topic branch based on the base branch
git worktree add worktrees/<topic-name> -b <project-name>/<topic-name> base/<project-name>

Example with 3 topics:

bash
git worktree add worktrees/topicA -b marker-fix/topicA base/marker-fix
git worktree add worktrees/topicB -b marker-fix/topicB base/marker-fix
git worktree add worktrees/topicC -b marker-fix/topicC base/marker-fix

Step 3: Environment Setup (if needed)

If the project has environment files, symlink them into each worktree:

bash
for wt in worktrees/*/; do
  ln -sf "$(pwd)/.env" "$wt/.env" 2>/dev/null
  # Add project-specific symlinks as needed (e.g., metadata, generated files)
done

If using pnpm workspaces, install dependencies in each worktree:

bash
for wt in worktrees/*/; do
  (cd "$wt" && pnpm install)
done

Step 4: Launch Child Sessions

Tell the user to start a new Claude Code session in each worktree directory:

code
cd worktrees/<topic-name>
claude

Or use Claude Code teams with the Task tool to spawn child agents in each worktree.

Step 5: Review and Merge Topic PRs

As child sessions complete work and create PRs:

bash
# Review each topic PR
gh pr view <pr-number>
gh pr diff <pr-number>

# Merge topic PRs into base branch (regular merge, not squash)
gh pr merge <pr-number>

Step 6: Create Root PR

After all topics are merged into the base branch:

bash
# Create the root PR: base → main
gh pr create --base main --head base/<project-name> \
  --title "Root PR title" \
  --body "$(cat <<'EOF'
## Summary
- Merged topicA: description
- Merged topicB: description
- Merged topicC: description

## Topic PRs
- #N topicA
- #N topicB
- #N topicC
EOF
)"

Step 7: Cleanup

After the root PR is merged:

bash
# Remove worktrees
for wt in worktrees/*/; do
  git worktree remove "$wt"
done

# Delete local and remote topic branches
git branch -d <project-name>/topicA <project-name>/topicB <project-name>/topicC
git push origin --delete <project-name>/topicA <project-name>/topicB <project-name>/topicC

# Delete base branch
git branch -d base/<project-name>
git push origin --delete base/<project-name>

Child Session Workflow

Step 1: Verify Branch

bash
git branch --show-current
# Should show: <project-name>/<topic-name>

Step 2: Implement

Work normally — edit files, run tests, commit.

Step 3: Push and Create PR

bash
git push -u origin <project-name>/<topic-name>

# Create PR targeting the base branch
gh pr create --base base/<project-name> \
  --title "Topic: description" \
  --body "Description of changes"

Branch Naming Conventions

TypePatternExample
Base branchbase/<project>base/marker-fix
Topic branch<project>/<topic>marker-fix/bogaudio-knobs
Worktree dirworktrees/<topic>worktrees/bogaudio-knobs

Important Rules

  1. Always pull main before creating the base branch — stale bases cause conflicts
  2. Never force push — regular merge only, preserves history
  3. Topic PRs target the base branch, not main
  4. Root PR targets main from the base branch
  5. worktrees/ must be in .gitignore — worktrees are local only
  6. Manager session stays at repo root — never cd into worktrees for git ops
  7. Each child session stays in its worktree — git ops affect that branch only

Prerequisites

  • worktrees/ in .gitignore
  • gh CLI authenticated
  • git version 2.15+ (worktree support)

Quick Reference

bash
# Manager: full setup
git checkout main && git pull origin main
git checkout -b base/my-project && git push -u origin base/my-project && git checkout main
git worktree add worktrees/topic1 -b my-project/topic1 base/my-project
git worktree add worktrees/topic2 -b my-project/topic2 base/my-project

# Child: push and PR
git push -u origin my-project/topic1
gh pr create --base base/my-project --title "Topic1: description"

# Manager: merge topics and create root PR
gh pr merge <topic1-pr> && gh pr merge <topic2-pr>
gh pr create --base main --head base/my-project --title "My Project: root PR"

# Manager: cleanup after root PR merged
git worktree remove worktrees/topic1 && git worktree remove worktrees/topic2