| name | cat:merge-subagent |
| description | Merge subagent branch into task branch with conflict resolution and cleanup |
Merge Subagent
Purpose
Integrate a completed subagent's work back into the parent task branch. Handles the merge process,
resolves conflicts if necessary, cleans up the subagent branch and worktree, and updates tracking
state.
When to Use
- After
collect-results confirms subagent work is ready
- Subagent has completed its task successfully
- Partial results from interrupted subagent need preservation
- Ready to consolidate subagent work into main task flow
Concurrent Execution Safety
This skill operates under the task lock held by /cat:work. Refresh the lock heartbeat for
long-running merge operations:
"${CLAUDE_PLUGIN_ROOT}/scripts/task-lock.sh" heartbeat "${CLAUDE_PROJECT_DIR}" "$TASK_ID" "${CLAUDE_SESSION_ID}"
The task lock is released by work cleanup step after all subagent work is merged.
Workflow
1. Verify Prerequisites
SUBAGENT_ID="a1b2c3d4"
TASK="1.2-implement-parser"
SUBAGENT_BRANCH="${TASK}-sub-${SUBAGENT_ID}"
TASK_BRANCH="${TASK}"
WORKTREE=".worktrees/${SUBAGENT_BRANCH}"
git branch --list "${TASK_BRANCH}"
git branch --list "${SUBAGENT_BRANCH}"
2. Checkout Task Branch
cd /workspace
git status --porcelain
git checkout "${TASK_BRANCH}"
git pull origin "${TASK_BRANCH}" 2>/dev/null || true
3. Merge Subagent Branch
git merge "${SUBAGENT_BRANCH}" --no-edit -m "Merge subagent ${SUBAGENT_ID}: ${TASK}"
4. Handle Conflicts (If Any)
If merge fails with conflicts:
git diff --name-only --diff-filter=U
git checkout --theirs path/to/file.java
git checkout --ours path/to/file.java
git add path/to/file.java
git commit -m "Merge subagent ${SUBAGENT_ID} with conflict resolution"
Conflict Resolution Strategy:
- Code files: Prefer subagent version (they have fresher context)
- Config files: Manual merge required
- State files: Merge metadata, keep both contributions
- Tests: Include all tests from both branches
5. Verify Merge Success
git log -1 --format="%s"
git status --porcelain
./gradlew compileJava 2>/dev/null || true
6. Delete Subagent Branch
git branch -d "${SUBAGENT_BRANCH}"
git push origin --delete "${SUBAGENT_BRANCH}" 2>/dev/null || true
7. Cleanup Subagent Worktree
git worktree remove "${WORKTREE}" --force
git worktree list | grep -v "${SUBAGENT_BRANCH}"
rm -rf "${WORKTREE}" 2>/dev/null || true
8. Update Parent STATE.md
subagents:
- id: a1b2c3d4
task: 1.2-implement-parser
status: merged
merged_at: 2026-01-10T15:30:00Z
merge_commit: abc123def456
conflicts_resolved: 0
worktree_cleaned: true
branch_deleted: true
Examples
Clean Merge (No Conflicts)
git checkout 1.2-implement-parser
git merge 1.2-implement-parser-sub-a1b2c3d4 -m "Merge subagent a1b2c3d4"
git branch -d 1.2-implement-parser-sub-a1b2c3d4
git worktree remove .worktrees/1.2-implement-parser-sub-a1b2c3d4
Merge with Conflicts
git merge 1.2-implement-parser-sub-a1b2c3d4
git checkout --theirs src/Parser.java
git add src/Parser.java
git commit -m "Merge subagent a1b2c3d4, resolved Parser.java conflict"
git branch -d 1.2-implement-parser-sub-a1b2c3d4
git worktree remove .worktrees/1.2-implement-parser-sub-a1b2c3d4
Merge Multiple Subagents
for subagent in "${SUBAGENTS_ORDERED[@]}"; do
git merge "${subagent}" -m "Merge ${subagent}"
git branch -d "${subagent}"
done
for worktree in "${WORKTREES[@]}"; do
git worktree remove "${worktree}" --force
done
Anti-Patterns
Always collect results before merging
git merge "${SUBAGENT_BRANCH}"
collect-results "${SUBAGENT_ID}"
merge-subagent "${SUBAGENT_ID}"
Include worktree cleanup with merge
git merge "${SUBAGENT_BRANCH}"
git branch -d "${SUBAGENT_BRANCH}"
git merge "${SUBAGENT_BRANCH}"
git branch -d "${SUBAGENT_BRANCH}"
git worktree remove "${WORKTREE}"
Understand conflicts before force-resolving
git merge -X theirs "${SUBAGENT_BRANCH}"
git merge "${SUBAGENT_BRANCH}"
git diff --name-only --diff-filter=U
Merge to task branch first (not directly to main)
git checkout main
git merge 1.2-parser-sub-a1b2c3d4
git checkout 1.2-implement-parser
git merge 1.2-parser-sub-a1b2c3d4
Verify merge before deleting branch
git branch -D "${SUBAGENT_BRANCH}"
git branch -d "${SUBAGENT_BRANCH}"
git branch --merged | grep "${SUBAGENT_BRANCH}" && git branch -d "${SUBAGENT_BRANCH}"
Related Skills
cat:collect-results - Must run before merge
cat:spawn-subagent - Creates subagent that will later be merged
cat:parallel-execute - Orchestrates multiple subagent merges
cat:monitor-subagents - Verify subagent complete before merge