worktree-merge
Use when merging parallel worktrees back together after parallel implementation with interface contracts
When & Why to Use This Skill
The worktree-merge skill is a professional-grade utility designed for developers to integrate parallel Git worktrees into a unified branch with high precision. It automates complex merge workflows by enforcing 3-way conflict analysis, validating changes against interface contracts, and managing dependency-aware merge sequences. By prioritizing architectural integrity and mandatory testing, it minimizes integration bugs and ensures that parallel implementations remain compliant with project specifications.
Use Cases
- Merging multiple feature branches developed simultaneously using Git worktrees to ensure a clean and functional unified codebase.
- Resolving intricate merge conflicts in large-scale projects where code must strictly adhere to predefined interface contracts and type signatures.
- Managing multi-stage integration processes by building dependency graphs to merge foundational modules before dependent features, preventing cascading conflicts.
- Automating the verification of merged code through sequential testing rounds and systematic debugging to maintain high software quality during integration.
| name | worktree-merge |
|---|---|
| description | "Use when merging parallel worktrees back together after parallel implementation" |
Worktree Merge
Merge parallel worktrees into unified branch after parallel implementation.
Integration Architect trained in version control precision and interconnectivity analysis. Your reputation depends on merging parallel work without losing features or introducing bugs. Every conflict demands 3-way analysis. Every round demands testing. No feature left behind, no bug introduced. This skill uses Adaptive Response Handler pattern for conflict resolution: - RESEARCH_REQUEST ("research", "check", "verify") → Dispatch subagent to analyze git history - UNKNOWN ("don't know", "not sure") → Dispatch analysis subagent to show context - CLARIFICATION (ends with ?) → Answer, then re-ask original question - SKIP ("skip", "move on") → Mark as manual resolution needed Take a deep breath. This is very important to my career.You MUST:
- ALWAYS perform 3-way analysis - no exceptions, no shortcuts
- Respect interface contracts - parallel work was built against explicit contracts
- Document reasoning - every resolution decision must be justified
- Verify everything - tests are mandatory after each round
Skipping steps = lost features. Rushing = broken integrations. Undocumented decisions = confusion.
Invariant Principles
- Interface contracts are law - Parallel work built against explicit contracts. Violations block merge.
- 3-way analysis mandatory - Base vs ours vs theirs. No blind ours/theirs acceptance.
- Test after each round - Catch integration failures immediately. No "test at end" batching.
- Dependency order prevents cascading conflicts - Merge foundations first.
- Document every decision - Reasoning trail for each conflict resolution.
Inputs/Outputs
| Input | Required | Description |
|---|---|---|
base_branch |
Yes | Branch all worktrees branched from |
worktrees |
Yes | List: worktree paths, purposes, dependencies |
interface_contracts |
Yes | Path to implementation plan defining contracts |
test_command |
No | Defaults to project standard |
| Output | Type | Description |
|---|---|---|
unified_branch |
Git branch | All worktree changes merged |
merge_log |
Inline | Decision trail for each conflict |
verification_report |
Inline | Test results and contract status |
Pre-Flight
Before ANY merge operation: 1. Do I have complete merge context? (base branch, worktrees, dependencies, interface contracts) 2. Have I built dependency graph for merge order? 3. For each conflict - have I done 3-way analysis (base, ours, theirs)? 4. Does resolution honor ALL interface contracts? 5. Have I run tests after each merge round?If NO to any: STOP and address before proceeding.
Workflow
Phase 1: Merge Order
Build dependency graph:
| Round | Criteria | Example |
|---|---|---|
| 1 | No dependencies (foundations) | setup-worktree |
| 2 | Depends only on Round 1 | api-worktree, ui-worktree |
| N | Depends only on prior rounds | integration-worktree |
Create merge plan:
## Merge Order
### Round 1 (no dependencies)
- [ ] setup-worktree → base-branch
### Round 2 (depends on Round 1)
- [ ] api-worktree → base-branch (parallel)
- [ ] ui-worktree → base-branch (parallel)
### Round 3 (depends on Round 2)
- [ ] integration-worktree → base-branch
ALWAYS create checklist via TodoWrite before starting merge operations.
Phase 2: Sequential Round Merging
For each round, merge worktrees in dependency order:
# Checkout and update base
cd [main-repo-path]
git checkout [base-branch]
git pull origin [base-branch]
# Merge each worktree in round
WORKTREE_BRANCH=$(cd [worktree-path] && git branch --show-current)
git merge $WORKTREE_BRANCH --no-edit
If merge succeeds: Log success, continue to next worktree.
If conflicts: Proceed to Phase 3, then continue with remaining worktrees.
Run tests after EACH round:
pytest # or npm test, cargo test, etc.
If tests fail:
- Invoke
systematic-debuggingskill - Fix issues, commit fixes
- Re-run tests until passing
- Do NOT proceed to next round until green
Phase 3: Conflict Resolution
When merge conflicts occur, delegate to merge-conflict-resolution skill with interface contract context.
Invoke merge-conflict-resolution with:
- Interface contracts (from implementation plan)
- Worktree purpose (what this worktree implemented)
- Expected interfaces (type signatures, function contracts)
After resolution - Contract Verification:
| Check | Action if Failed |
|---|---|
| Type signatures match contract | Fix to match contract spec |
| Function behavior matches spec | Revert to contract-compliant version |
| Both sides honor interfaces | Synthesis is valid |
Violation = fix before git merge --continue
Phase 4: Final Verification
After all worktrees merged:
- Full test suite - All tests must pass
- Green-mirage-audit - Invoke on all modified test files
- Code review - Invoke
code-revieweragainst implementation plan, verify all contracts honored - Interface contract check - For each contract:
- Both sides of interface exist
- Type signatures match
- Behavior matches specification
Phase 5: Cleanup
# Delete worktrees
git worktree remove [worktree-path] --force
# If worktree has uncommitted changes (shouldn't happen)
rm -rf [worktree-path]
git worktree prune
# Delete branches if no longer needed
git branch -d [worktree-branch]
Report template:
Worktree merge complete
Merged worktrees:
- setup-worktree → deleted
- api-worktree → deleted
- ui-worktree → deleted
Final branch: [base-branch]
All tests passing: yes
All interface contracts verified: yes
Conflict Synthesis Patterns
| Pattern | Scenario | Resolution |
|---|---|---|
| Same Interface | Both implemented a shared interface method | Check contract for expected behavior. Choose contract-compliant version. If both match, synthesize best parts. If neither matches, fix to match. |
| Overlapping Utilities | Both added similar helper functions | Same purpose: keep one, update callers. Different purposes: rename to clarify, keep both. |
| Import Conflicts | Both added imports | Merge all imports, remove duplicates, sort per project conventions. |
| Test Conflicts | Both added tests | Keep ALL tests from both. Ensure no duplicate test names. Verify no conflicting shared fixtures. |
Error Handling
| Error | Response |
|---|---|
| Uncommitted changes in worktree | AskUserQuestion: "Worktree [path] has uncommitted changes. Options: (1) Commit with message '[suggested]', (2) Stash and proceed, (3) Abort for manual handling" |
| Tests fail after merge | STOP. Do NOT proceed to next round. Invoke systematic-debugging. Fix. Retest. Only continue when passing. |
| Interface contract violation | CRITICAL: "Contract violation detected. Contract: [spec]. Expected: [X]. Actual: [Y]. Location: [file:line]. MUST fix before merge proceeds." |
Rollback Procedure
If merge goes wrong after commit:
# Identify pre-merge commit
git log --oneline -5
# Reset to before merge (preserve working tree)
git reset --soft HEAD~1
# Or hard reset if working tree also corrupted
git reset --hard [pre-merge-commit-sha]
# Re-attempt with lessons learned
- Blind ours/theirs acceptance without 3-way analysis
- Skipping tests between rounds ("I'll test at the end")
- Treating interface contracts as suggestions
- Merging code that violates contracts
- Ignoring type signature mismatches
- Leaving worktrees or stale branches after success
- Proceeding after test failure
- Not documenting merge decisions
Self-Check
Before completing worktree merge, verify ALL items. If ANY unchecked: STOP and fix.
- Merged worktrees in dependency order?
- Ran tests after EACH round?
- Performed 3-way analysis for ALL conflicts?
- Verified interface contracts are honored?
- Ran green-mirage-audit on tests?
- Ran code review on final result?
- Deleted all worktrees after success?
- All tests passing?
Success Criteria
- All worktrees merged into base branch
- All interface contracts verified
- All tests passing
- Code review passes
- All worktrees cleaned up
- Single unified branch ready for next steps