agentleFS
axiomanticaxiomantic/spellbook/skills/merging-worktrees/SKILL.md

merging-worktrees

Skill10 starsChanged 4 months agoMITView on GitHub

Use when merging parallel worktrees back together after parallel implementation, combining parallel development tracks, or unifying branches from dispatched parallel agents. Triggers: 'merge worktrees', 'combine parallel branches', 'integrate parallel work', 'all tracks complete', 'bring everything together'.

  • Deletes or force-pushes

The file

Show its rules and text

We split this skill into the rules it gives an agent

21 rules. About 0 are common, 20 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈20

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    ALWAYS perform 3-way analysis - no exceptions, no shortcuts

    line 28
  2. Distinctive:

    Document reasoning - every resolution decision must be justified

    line 30
  3. Distinctive:

    Verify everything - tests are mandatory after each round

    line 31
  4. Very specific:

    Before resolving ANY merge conflict, the subagent handling resolution MUST have the resolving-merge-conflicts skill loaded.

    line 47
  5. Distinctive:

    When dispatching a conflict resolution subagent:

    line 49
  6. Distinctive:

    Do NOT resolve conflicts inline in the orchestrator context

    line 52
  7. Distinctive:

    If you catch yourself resolving a conflict without having loaded the skill: STOP.

    line 54
  8. Distinctive:

    Before ANY merge operation:

    line 75
  9. Distinctive:

    Do I have complete merge context?

    line 76
  10. Distinctive:

    If NO to any: STOP and address before proceeding.

    line 82
  11. Distinctive:

    Never present option 4 as "stash and proceed" with no impact statement.

    line 175
  12. Distinctive:

    If the operator chooses option 3 or 4, VERIFY afterwards — a stash round-trip damages staged, modified, and untracked files alike, and it …

    line 180
  13. Distinctive:

    If merge goes wrong after commit:

    line 201
  14. Distinctive:

    Prefer the non-destructive reset.

    line 203
  15. Distinctive:

    Before proposing it, enumerate exactly what it destroys:

    line 220
  16. Distinctive:

    Run it only after an explicit confirmation.

    line 235
  17. Distinctive:

    Not documenting merge decisions

    line 247
  18. Distinctive:

    After each phase, verify: outputs produced, quality gates passed, no unresolved merge conflicts or test failures remaining.

    line 269
  19. Distinctive:

    All worktrees merged into base branch

    line 274
  20. Distinctive:

    All interface contracts verified

    line 275
  21. Distinctive:

    All worktrees cleaned up, each removal confirmed by the user first

    line 278

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Worktree Merge

Merge parallel worktrees into unified branch after parallel implementation.

<ROLE> 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. </ROLE>

<ARH_INTEGRATION> Adaptive Response Handler for conflict resolution dialogue:

  • 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 </ARH_INTEGRATION>
<CRITICAL> Take a deep breath. This is very important to my career.

MUST:

  1. ALWAYS perform 3-way analysis - no exceptions, no shortcuts
  2. Respect interface contracts - parallel work was built against explicit contracts
  3. Document reasoning - every resolution decision must be justified
  4. Verify everything - tests are mandatory after each round

Skipping steps = lost features. Rushing = broken integrations. Undocumented decisions = confusion. </CRITICAL>

Invariant Principles

  1. Interface contracts are law - Parallel work built against explicit contracts. Violations block merge.
  2. 3-way analysis mandatory - Base vs ours vs theirs. No blind ours/theirs acceptance.
  3. Test after each round - Catch integration failures immediately. No "test at end" batching.
  4. Dependency order prevents cascading conflicts - Merge foundations first.
  5. Document every decision - Reasoning trail for each conflict resolution.

Pre-Conflict Gate

<CRITICAL> Before resolving ANY merge conflict, the subagent handling resolution MUST have the `resolving-merge-conflicts` skill loaded. Conflicts resolved without it default to LLM base-model bias toward "pick the simpler option" (ours/theirs selection, not synthesis).

When dispatching a conflict resolution subagent:

  1. Subagent prompt MUST instruct it to invoke resolving-merge-conflicts via the Skill tool
  2. Subagent prompt MUST include interface contract context from the implementation plan
  3. Do NOT resolve conflicts inline in the orchestrator context

If you catch yourself resolving a conflict without having loaded the skill: STOP. Dispatch a subagent that loads it. </CRITICAL>

Inputs/Outputs

InputRequiredDescription
base_branchYesBranch all worktrees branched from
worktreesYesList: worktree paths, purposes, dependencies
interface_contractsYesPath to implementation plan defining contracts
test_commandNoDefaults to project standard
OutputTypeDescription
unified_branchGit branchAll worktree changes merged
merge_logInlineDecision trail for each conflict
verification_reportInlineTest results and contract status

Pre-Flight

<analysis> 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. </analysis>

Workflow

Phase 1: Merge Order

Build dependency graph:

RoundCriteriaExample
1No dependencies (foundations)setup-worktree
2Depends only on Round 1api-worktree, ui-worktree
NDepends only on prior roundsintegration-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

<RULE>ALWAYS create checklist via TodoWrite before starting merge operations.</RULE>

Phase 2: Sequential Round Merging

Dispatch: /merge-worktree-execute

Phase 3: Conflict Resolution

Dispatch: /merge-worktree-resolve

Phases 4-5: Final Verification + Cleanup

Dispatch: /merge-worktree-verify

Conflict Synthesis Patterns

PatternScenarioResolution
Same InterfaceBoth implemented a shared interface methodCheck contract for expected behavior. Choose contract-compliant version. If both match, synthesize best parts. If neither matches, fix to match.
Overlapping UtilitiesBoth added similar helper functionsSame purpose: keep one, update callers. Different purposes: rename to clarify, keep both.
Import ConflictsBoth added importsMerge all imports, remove duplicates, sort per project conventions.
Test ConflictsBoth added testsKeep ALL tests from both. Ensure no duplicate test names. Verify no conflicting shared fixtures.

Error Handling

ErrorResponse
Uncommitted changes in worktreeDo NOT stash by reflex. Follow the procedure below.
Tests fail after mergeSTOP. Do NOT proceed to next round. Invoke systematic-debugging. Fix. Retest. Only continue when passing.
Interface contract violationCRITICAL: "Contract violation detected. Contract: [spec]. Expected: [X]. Actual: [Y]. Location: [file:line]. MUST fix before merge proceeds."

Uncommitted Changes in a Worktree

Show what is actually at risk before offering any option:

git -C [worktree-path] status --porcelain -uall
git -C [worktree-path] diff              # Unstaged
git -C [worktree-path] diff --staged     # Staged

To inspect committed content for comparison, read it; do not mutate the tree:

git show HEAD:<path>

Then ask via AskUserQuestion, in this order — least destructive first:

  • question: Worktree [path] has uncommitted changes. / Effect of each option below / Recoverable: varies by option, stated per option
  • options:
    1. Commit them — nothing is discarded; message [suggested]. Recoverable.
    2. Abort for manual handling — nothing is touched. Recoverable.
    3. Set aside specific files — name the EXACT paths; never ., never a bare directory. Scope is limited to the paths you name.
    4. Stash the whole tree — LAST RESORT. git stash is TREE-WIDE: it captures every uncommitted change in the checkout, not only the files being merged. In a checkout shared with another agent it will take work the operator did not author and cannot see. Recovery is git stash list then git stash pop — but a pop CONFLICT can leave the tree in a partial state, half-applied and with the stash entry still present.
<CRITICAL> Never present option 4 as "stash and proceed" with no impact statement. An operator reading those words pictures their own edits being set aside and restored; they are not picturing another agent's uncommitted work being swept up. A confirmation obtained without the impact statement is not a confirmation.

If the operator chooses option 3 or 4, VERIFY afterwards — a stash round-trip damages staged, modified, and untracked files alike, and it fails silently:

files=$(git status --porcelain -uall) || { echo "sweep failed: git status errored"; exit 1; }
printf '%s\n' "$files" | grep -v '^D \|^.D' | sed 's/^...//;s/.* -> //' |
while IFS= read -r f; do
  case "$f" in *__init__.py|*.gitkeep|*/py.typed) continue;; esac
  [ -f "$f" ] && [ ! -s "$f" ] && echo "TRUNCATED: $f"
done
echo "sweep complete"

Any name it prints that you did not deliberately empty is a truncation. Output of only sweep complete is clean. git status is checked explicitly: if it errors, the sweep prints sweep failed and exits 1 rather than silently reporting an empty result as clean. </CRITICAL>

Rollback Procedure

If merge goes wrong after commit:

Prefer the non-destructive reset. It rewinds the commit and KEEPS the working tree:

# Identify pre-merge commit
git log --oneline -5

# Reset to before merge (preserve working tree)
git reset --soft HEAD~1

# Re-attempt with lessons learned
<CRITICAL> `git reset --hard` DISCARDS every uncommitted change in the tree, including work you did not author, and the reflog does not bring it back. Never run it as a routine next step when `--soft` did not obviously suffice.

Before proposing it, enumerate exactly what it destroys:

git status --porcelain -uall

Then ask via AskUserQuestion, never as prose:

  • question: Running: git reset --hard [pre-merge-commit-sha] / Effect: discards the uncommitted changes listed above and every commit after [pre-merge-commit-sha] / Recoverable: commits via reflog; uncommitted changes NOT recoverable
  • options: Run it (uncommitted work destroyed) and Cancel (keep the tree, fix forward).

Run it only after an explicit confirmation. Never run it and describe the impact afterwards. </CRITICAL>

<FORBIDDEN> - 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 is confirmed AND the user has approved their removal - Proceeding after test failure - Not documenting merge decisions - Deleting worktrees or branches before explicit user confirmation - Running `git reset --hard` without first stating what it discards and getting an explicit confirmation via AskUserQuestion - Running `git stash` in a checkout that may hold uncommitted work you did not author, without an impact statement made before the operator answers - Offering "stash and proceed" as an option without disclosing that `git stash` is tree-wide - Skipping the truncation sweep after a stash or a set-aside operation </FORBIDDEN>

Self-Check

<RULE>Before completing worktree merge, verify ALL items. If ANY unchecked: STOP and fix.</RULE>

  • Merged worktrees in dependency order?
  • Ran tests after EACH round?
  • Performed 3-way analysis for ALL conflicts?
  • Verified interface contracts are honored?
  • Ran auditing-green-mirage on tests?
  • Ran code review on final result?
  • Confirmed worktree removal with the user, and deleted only what they approved?
  • All tests passing?
<reflection> After each phase, verify: outputs produced, quality gates passed, no unresolved merge conflicts or test failures remaining. </reflection>

Success Criteria

  • All worktrees merged into base branch
  • All interface contracts verified
  • All tests passing
  • Code review passes
  • All worktrees cleaned up, each removal confirmed by the user first
  • Single unified branch ready for next steps

<FINAL_EMPHASIS> Your reputation depends on merging parallel work without losing features or introducing bugs. Every conflict requires 3-way analysis. Every round requires testing. Every merge requires verification. Interface contracts are mandatory, not suggestions. No feature left behind. No bug introduced. You'd better be sure. </FINAL_EMPHASIS>

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.