local-merge
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese/local-merge
/local-merge
Merge a branch into a target branch via a disposable shallow clone, then propagate to the primary worktree non-destructively. Defaults to main if no target specified.
通过一次性浅克隆将分支合并到目标分支,然后无损同步到主工作区。若未指定目标分支,默认使用main。
Inputs
输入项
| Input | Required | Default | Example |
|---|---|---|---|
| yes | — | |
| no | | |
| no | First line of | |
| no | | |
| 输入项 | 是否必填 | 默认值 | 示例 |
|---|---|---|---|
| 是 | — | |
| 否 | | |
| 否 | | |
| 否 | | |
Phase 1 — Remote Merge (mechanical)
阶段1 — 远程合并(机械操作)
All commands are explicit. No reasoning required.
所有命令均为显式执行,无需推理。
1a. Calculate clone depth
1a. 计算克隆深度
bash
git fetch origin "$TARGET" "$BRANCH"
DEPTH=$(( $(git rev-list --count origin/"$TARGET"..origin/"$BRANCH") + 10 ))bash
git fetch origin "$TARGET" "$BRANCH"
DEPTH=$(( $(git rev-list --count origin/"$TARGET"..origin/"$BRANCH") + 10 ))1b. Clone, merge, push
1b. 克隆、合并、推送
bash
MERGE_DIR="${CLAUDE_SESSION_DIR:-/tmp}/local-merge-$$"
[ -d "$MERGE_DIR" ] && rm -rf "$MERGE_DIR"
git clone --depth "$DEPTH" --branch "$TARGET" "$(git remote get-url origin)" "$MERGE_DIR"
git -C "$MERGE_DIR" fetch origin "$BRANCH"
git -C "$MERGE_DIR" merge FETCH_HEAD -m "$MESSAGE"
git -C "$MERGE_DIR" pushbash
MERGE_DIR="${CLAUDE_SESSION_DIR:-/tmp}/local-merge-$$"
[ -d "$MERGE_DIR" ] && rm -rf "$MERGE_DIR"
git clone --depth "$DEPTH" --branch "$TARGET" "$(git remote get-url origin)" "$MERGE_DIR"
git -C "$MERGE_DIR" fetch origin "$BRANCH"
git -C "$MERGE_DIR" merge FETCH_HEAD -m "$MESSAGE"
git -C "$MERGE_DIR" push1c. Non-fast-forward recovery
1c. 非快进恢复
bash
git -C "$MERGE_DIR" pull --rebase
git -C "$MERGE_DIR" pushMax 3 retries. After 3 failures, stop and report. Do not force-push.
bash
git -C "$MERGE_DIR" pull --rebase
git -C "$MERGE_DIR" push最多重试3次。3次失败后停止并上报,禁止强制推送。
1d. Cleanup
1d. 清理
Always runs after Phase 1, success or failure:
bash
rm -rf "$MERGE_DIR"无论阶段1成功或失败,都会执行此步骤:
bash
rm -rf "$MERGE_DIR"Phase 2 — Propagate to Primary (reasoning required)
阶段2 — 同步到主工作区(需推理)
Bring the primary worktree's up to date with the newly-pushed remote. Steps 2a-2c are mechanical. Step 2d requires agent judgment. Steps 2e-2g depend on that judgment.
$TARGET将主工作区的分支与新推送的远程分支同步。步骤2a-2c为机械操作,步骤2d需要Agent判断,步骤2e-2g取决于该判断结果。
$TARGET2a. Guard: is primary on TARGET?
2a. 检查:主工作区是否在TARGET分支?
bash
git -C "$PRIMARY" branch --show-currentNot on ? Stop. Report: "Primary is on , not . Remote updated; local not forwarded." Done.
$TARGET<branch>$TARGETbash
git -C "$PRIMARY" branch --show-current不在分支?停止操作。 上报:“主工作区当前在分支,而非分支。远程分支已更新;本地未同步。”操作结束。
$TARGET<branch>$TARGET2b. Protect dirty state
2b. 保护未提交状态
bash
git -C "$PRIMARY" status --porcelainIf output is non-empty, shelter uncommitted work:
bash
git -C "$PRIMARY" add -A
git -C "$PRIMARY" commit -m "wip: preserve local state before remote sync"Record for step 2g. If clean, skip.
WIP_CREATED=truebash
git -C "$PRIMARY" status --porcelain若输出非空,保存未提交工作:
bash
git -C "$PRIMARY" add -A
git -C "$PRIMARY" commit -m "wip: preserve local state before remote sync"记录以便步骤2g使用。若工作区干净,跳过此步骤。
WIP_CREATED=true2c. Fetch updated TARGET
2c. 获取更新后的TARGET分支
bash
git -C "$PRIMARY" fetch origin "$TARGET"bash
git -C "$PRIMARY" fetch origin "$TARGET"2d. Analyze divergence (agent reasoning)
2d. 分析分歧(Agent推理)
Run all five commands, then assess using the decision matrix:
bash
git -C "$PRIMARY" rev-list --count HEAD..origin/"$TARGET" # commits behind
git -C "$PRIMARY" rev-list --count origin/"$TARGET"..HEAD # commits ahead
git -C "$PRIMARY" diff HEAD..origin/"$TARGET" --stat # changed files
git -C "$PRIMARY" log HEAD..origin/"$TARGET" --oneline # incoming commits
git -C "$PRIMARY" log origin/"$TARGET"..HEAD --oneline # local-only commits| Behind | Ahead | Assessment | Action |
|---|---|---|---|
| N > 0 | 0 | Fast-forward | |
| N > 0 | M > 0 | Diverged | Inspect diff for file overlap + semantic risk (2e) |
| 0 | 0 | Already current | Skip, report, done |
| 0 | M > 0 | Local-only commits | Unexpected — escalate to copilot (2f) |
Beyond textual conflicts, assess semantic risk: two agents editing related config in different files can silently break things even when git sees no conflict.
执行以下五条命令,然后根据决策矩阵评估:
bash
git -C "$PRIMARY" rev-list --count HEAD..origin/"$TARGET" # 落后的提交数
git -C "$PRIMARY" rev-list --count origin/"$TARGET"..HEAD # 超前的提交数
git -C "$PRIMARY" diff HEAD..origin/"$TARGET" --stat # 变更文件统计
git -C "$PRIMARY" log HEAD..origin/"$TARGET" --oneline # 待合并提交记录
git -C "$PRIMARY" log origin/"$TARGET"..HEAD --oneline # 本地独有提交记录| 落后数 | 超前数 | 评估结果 | 操作 |
|---|---|---|---|
| N > 0 | 0 | 可快进 | |
| N > 0 | M > 0 | 分支分歧 | 检查文件重叠情况及语义风险(步骤2e) |
| 0 | 0 | 已同步 | 跳过,上报结果,操作结束 |
| 0 | M > 0 | 本地独有提交 | 异常情况 — 升级至Copilot处理(步骤2f) |
除了文本冲突外,还需评估语义风险:两个Agent编辑不同文件中的相关配置时,即使Git未检测到冲突,也可能导致静默故障。
2e. Execute merge
2e. 执行合并
Fast-forward (behind > 0, ahead = 0):
bash
git -C "$PRIMARY" merge --ff-only origin/"$TARGET"Non-conflicting divergence (different files, low semantic risk):
bash
git -C "$PRIMARY" merge origin/"$TARGET" --no-editIf merge produces conflicts, abort immediately:
bash
git -C "$PRIMARY" merge --abortThen escalate to copilot (2f).
Uncertain or risky: do not attempt merge. Go to 2f.
快进合并(落后数>0,超前数=0):
bash
git -C "$PRIMARY" merge --ff-only origin/"$TARGET"无冲突分歧(变更文件不同,语义风险低):
bash
git -C "$PRIMARY" merge origin/"$TARGET" --no-edit若合并产生冲突,立即终止:
bash
git -C "$PRIMARY" merge --abort然后升级至Copilot处理(步骤2f)。
不确定或高风险:不尝试合并,直接进入步骤2f。
2f. Copilot escalation
2f. 升级至Copilot
- Invoke to enter copilot mode
/copilot - Present the analysis from 2d: behind/ahead counts, diff stat, commit logs
- State what blocked automatic resolution
- Let the user drive the merge
- When resolved, invoke to restore previous mode
/autonomous
- 调用进入协作模式
/copilot - 提交步骤2d的分析结果:落后/超前提交数、差异统计、提交日志
- 说明阻止自动解决的原因
- 由用户主导合并操作
- 问题解决后,调用恢复之前的模式
/autonomous
2g. Restore WIP state
2g. 恢复WIP状态
Only if :
WIP_CREATED=truebash
git -C "$PRIMARY" reset --soft HEAD~1Preserves all changes in the index and working tree exactly as they were before the skill ran.
仅当时执行:
WIP_CREATED=truebash
git -C "$PRIMARY" reset --soft HEAD~1精确保留索引和工作区在本Skill运行前的所有变更。
Safety Invariants
安全准则
- No force-push. Non-fast-forward retries use , never
pull --rebase.--force - No work lost. Dirty state is WIP-committed before any merge, restored after.
- No direct commits to main. Phase 1 uses a disposable clone for multi-agent isolation regardless of target. Phase 2 only fast-forwards or merges from origin.
- Conflict = stop. Conflicts trigger and copilot escalation, never auto-resolution.
merge --abort - Cleanup always runs. The shallow clone is removed regardless of outcome.
- 禁止强制推送。非快进重试使用,绝不使用
pull --rebase。--force - 无工作丢失。合并前将未提交状态通过WIP提交保存,操作后恢复。
- 禁止直接向main提交。无论目标分支是什么,阶段1均使用一次性克隆实现多Agent隔离。阶段2仅从origin分支进行快进或合并。
- 冲突即停止。冲突触发并升级至Copilot,绝不自动解决。
merge --abort - 始终执行清理。无论结果如何,都会删除浅克隆目录。
Integration
集成场景
| Caller | Context |
|---|---|
| CI-down path after review approval |
| Post-task consolidation to main |
| Receives escalation from Phase 2f |
| Restored after copilot resolves conflict |
| 调用者 | 上下文 |
|---|---|
| 审核通过后的CI下游流程 |
| 任务完成后合并至main分支 |
| 接收阶段2f的升级请求 |
| Copilot解决冲突后恢复自动模式 |