reconcile

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Reconcile

协调整理

Consolidate worktrees into the main worktree and clean up stale branches for a project.
将工作树合并到主工作树,并清理项目的陈旧分支。

Arguments

参数

Accepts an optional project name as an argument:
  • /reconcile
    — reconcile the current working directory's project
  • /reconcile py-lintro
    — reconcile the project at
    ~/Code/py-lintro
接受可选的项目名称作为参数:
  • /reconcile
    — 协调当前工作目录的项目
  • /reconcile py-lintro
    — 协调位于
    ~/Code/py-lintro
    的项目

Workflow

工作流程

Phase 1 — Discovery

阶段1 — 发现

  1. Identify the project root:
    • If a project name is given, look for
      ~/Code/<project-name>
    • Otherwise, use the current git repository root
    • Confirm it's a git repository — if not, stop
  2. Find all worktrees:
    bash
    git worktree list
  3. Find sibling directories that may be related:
    • List directories matching
      <project-name>-*
      in the same parent directory
    • For each, check if it's:
      • A worktree (
        .git
        is a file pointing to the main repo)
      • A separate clone (
        .git
        is a directory with same remote)
      • Unrelated (different remote or not a git repo) — ignore these
  4. Collect all branches:
    bash
    git branch -vv --no-color
  1. 确定项目根目录:
    • 如果指定了项目名称,查找
      ~/Code/<project-name>
    • 否则,使用当前Git仓库根目录
    • 确认这是一个Git仓库 — 如果不是,停止操作
  2. 查找所有工作树:
    bash
    git worktree list
  3. 查找可能相关的同级目录:
    • 列出父目录中匹配
      <project-name>-*
      的目录
    • 对每个目录,检查它是否是:
      • 工作树
        .git
        是指向主仓库的文件)
      • 独立克隆
        .git
        是包含相同远程仓库的目录)
      • 无关目录(不同远程仓库或非Git仓库)—— 忽略此类目录
  4. 收集所有分支:
    bash
    git branch -vv --no-color

Phase 2 — Analysis

阶段2 — 分析

For each branch (excluding
main
/
master
), determine its status:
  1. Check remote tracking:
    • If tracking branch shows
      gone
      → remote branch was deleted (PR likely merged or closed)
    • If tracking branch exists → check PR status
  2. Check GitHub PR status (if
    gh
    is available):
    bash
    gh pr list --state all --json number,title,headRefName,state
    • Map each branch to its PR (if any)
    • Categorize:
      merged
      ,
      open
      ,
      closed
      (without merge), or
      no PR
  3. Check for uncommitted changes in each worktree:
    bash
    git -C <worktree-path> status --short
  4. Check for stashes:
    bash
    git stash list
  5. Check if main worktree is on
    main
    — flag if it's on a feature branch
对每个分支(排除
main
/
master
),确定其状态:
  1. 检查远程跟踪状态:
    • 如果跟踪分支显示
      gone
      → 远程分支已删除(PR可能已合并或关闭)
    • 如果跟踪分支存在 → 检查PR状态
  2. 检查GitHub PR状态(如果
    gh
    可用):
    bash
    gh pr list --state all --json number,title,headRefName,state
    • 将每个分支映射到对应的PR(如果存在)
    • 分类:
      merged
      (已合并)、
      open
      (开放)、
      closed
      (未合并关闭)或
      no PR
      (无PR)
  3. 检查每个工作树中的未提交更改:
    bash
    git -C <worktree-path> status --short
  4. 检查暂存内容:
    bash
    git stash list
  5. 检查主工作树是否处于
    main
    分支
    — 如果处于功能分支则标记

Phase 3 — Report

阶段3 — 报告

Present a summary to the user, grouped by action:
text
Project: turbo-themes
Main worktree: ~/Code/turbo-themes (on refactor/265 — should be main)

Worktrees to remove:
  ✅ turbo-themes-267-...  → fix/267-text-contrast (PR #350 merged, clean)
  ✅ turbo-themes-build-.. → fix/build-ci-site     (PR #359 merged, clean)
  ⚠️  turbo-themes-ci-...   → fix/ci-missing-rose   (PR #356 merged, 3 uncommitted files)

Branches to delete (PRs merged):
  fix/267-text-contrast-state-buttons-light-themes
  fix/build-ci-site-missing-core-dep
  fix/ci-missing-rose-pine-synced

Branches to keep:
  📌 feat/landing-page-redesign — no PR, unmerged work

Separate clones found:
  🔶 ~/Code/turbo-themes-test — separate clone, not a worktree

Stashes: 5 (all on merged branches)
向用户展示按操作分组的摘要:
text
项目: turbo-themes
主工作树: ~/Code/turbo-themes (位于 refactor/265 — 应切换到main)

待移除的工作树:
  ✅ turbo-themes-267-...  → fix/267-text-contrast (PR #350已合并,无未提交内容)
  ✅ turbo-themes-build-.. → fix/build-ci-site     (PR #359已合并,无未提交内容)
  ⚠️  turbo-themes-ci-...   → fix/ci-missing-rose   (PR #356已合并,存在3个未提交文件)

待删除的分支(PR已合并):
  fix/267-text-contrast-state-buttons-light-themes
  fix/build-ci-site-missing-core-dep
  fix/ci-missing-rose-pine-synced

需保留的分支:
  📌 feat/landing-page-redesign — 无PR,存在未合并工作内容

发现独立克隆:
  🔶 ~/Code/turbo-themes-test — 独立克隆,非工作树

暂存内容: 5个(均位于已合并分支)

Phase 4 — Confirm

阶段4 — 确认

Ask the user to confirm the plan (use a structured question tool if available):
  • Which branches to keep vs delete
  • Whether to remove separate clones
  • Whether to clear stale stashes
  • Confirm that uncommitted changes will be stashed
Do NOT proceed without explicit confirmation.
请求用户确认计划(如果可用,使用结构化提问工具):
  • 哪些分支需要保留或删除
  • 是否移除独立克隆
  • 是否清理陈旧暂存内容
  • 确认未提交更改将被暂存
未获得明确确认前,请勿执行操作。

Phase 5 — Execute

阶段5 — 执行

After confirmation, perform the cleanup in this order:
  1. Remove worktree directories:
    • Try
      git worktree remove <path>
      first
    • Before any fallback delete, resolve
      <path>
      to a canonical path and verify all of the following:
      • Path is non-empty and exists
      • Path is NOT
        /
        and NOT the home directory
      • Path is under an allowed prefix (repo sibling worktree/clone root)
      • Path is not a symlink escaping the allowed prefix
    • Only with explicit user confirmation, if removal still fails (orphaned directory), run
      rm -rf <path>
      then
      git worktree prune
  2. Stash uncommitted changes (if any, on branches being kept):
    bash
    git -C <worktree-path> stash push -m "reconcile: WIP on <branch-name>"
  3. Switch main worktree to the default branch:
    bash
    DEFAULT_BRANCH=$(git symbolic-ref --short refs/remotes/origin/HEAD | sed 's#^origin/##')
    git checkout "$DEFAULT_BRANCH"
    git pull
  4. Delete merged branches:
    bash
    git branch -d <branch1> <branch2> ...
  5. Clear stale stashes (only if user confirmed):
    • Drop stashes that reference deleted branches
  6. Remove separate clones (only if user confirmed):
    bash
    rm -rf <clone-path>
    • Apply the same canonical path-safety checks before deletion (non-empty, exists, not
      /
      or home, within allowed prefix, no symlink escape)
确认后,按以下顺序执行清理:
  1. 移除工作树目录:
    • 首先尝试
      git worktree remove <path>
    • 在执行任何备用删除操作前,将
      <path>
      解析为规范路径并验证以下所有条件:
      • 路径非空且存在
      • 路径不是
        /
        或主目录
      • 路径位于允许的前缀下(仓库同级工作树/克隆根目录)
      • 路径不是超出允许前缀的符号链接
    • 只有获得用户明确确认后,如果移除仍失败(孤立目录),才执行
      rm -rf <path>
      然后
      git worktree prune
  2. 暂存未提交更改(如果存在于需保留的分支):
    bash
    git -C <worktree-path> stash push -m "reconcile: WIP on <branch-name>"
  3. 将主工作树切换到默认分支:
    bash
    DEFAULT_BRANCH=$(git symbolic-ref --short refs/remotes/origin/HEAD | sed 's#^origin/##')
    git checkout "$DEFAULT_BRANCH"
    git pull
  4. 删除已合并分支:
    bash
    git branch -d <branch1> <branch2> ...
  5. 清理陈旧暂存内容(仅当用户确认):
    • 删除引用已删除分支的暂存内容
  6. 移除独立克隆(仅当用户确认):
    bash
    rm -rf <clone-path>
    • 删除前应用相同的规范路径安全检查(非空、存在、不是
      /
      或主目录、位于允许前缀下、无符号链接逃逸)

Phase 6 — Verify

阶段6 — 验证

Print the final state:
text
=== worktrees ===
~/Code/turbo-themes  abc1234 [main]

=== branches ===
  feat/landing-page-redesign  def5678  unmerged work
* main                        abc1234  [origin/main] latest commit message

=== stashes ===
(none)
打印最终状态:
text
=== 工作树 ===
~/Code/turbo-themes  abc1234 [main]

=== 分支 ===
  feat/landing-page-redesign  def5678  未合并工作内容
* main                        abc1234  [origin/main] 最新提交信息

=== 暂存内容 ===
(无)

Important

注意事项

  • NEVER delete branches or worktrees without asking the user first
  • ALWAYS stash uncommitted changes before cleanup
  • NEVER force-push or modify remote state
  • NEVER delete
    main
    or
    master
    branches
  • Always present the full picture before taking action
  • If a directory can't be identified as a worktree or clone, flag it and ask — don't assume
  • If
    gh
    is not available, fall back to checking remote tracking status only
  • Branches with open PRs should default to "keep" in the recommendation
  • Branches with no PR and no remote should be flagged for user decision
  • 未经用户确认,绝不能删除分支或工作树
  • 清理前必须暂存未提交更改
  • 绝不能强制推送或修改远程状态
  • 绝不能删除
    main
    master
    分支
  • 执行操作前必须向用户展示完整情况
  • 如果无法确定目录是工作树还是克隆,标记并询问用户 — 不要假设
  • 如果
    gh
    不可用,仅回退到检查远程跟踪状态
  • 带有开放PR的分支默认建议“保留”
  • 无PR且无远程分支的分支需标记供用户决策