reconcile
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReconcile
协调整理
Consolidate worktrees into the main worktree and clean up stale branches for a project.
将工作树合并到主工作树,并清理项目的陈旧分支。
Arguments
参数
Accepts an optional project name as an argument:
- — reconcile the current working directory's project
/reconcile - — reconcile the project at
/reconcile py-lintro~/Code/py-lintro
接受可选的项目名称作为参数:
- — 协调当前工作目录的项目
/reconcile - — 协调位于
/reconcile py-lintro的项目~/Code/py-lintro
Workflow
工作流程
Phase 1 — Discovery
阶段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
- If a project name is given, look for
-
Find all worktrees:bash
git worktree list -
Find sibling directories that may be related:
- List directories matching in the same parent directory
<project-name>-* - For each, check if it's:
- A worktree (is a file pointing to the main repo)
.git - A separate clone (is a directory with same remote)
.git - Unrelated (different remote or not a git repo) — ignore these
- A worktree (
- List directories matching
-
Collect all branches:bash
git branch -vv --no-color
-
确定项目根目录:
- 如果指定了项目名称,查找
~/Code/<project-name> - 否则,使用当前Git仓库根目录
- 确认这是一个Git仓库 — 如果不是,停止操作
- 如果指定了项目名称,查找
-
查找所有工作树:bash
git worktree list -
查找可能相关的同级目录:
- 列出父目录中匹配 的目录
<project-name>-* - 对每个目录,检查它是否是:
- 工作树(是指向主仓库的文件)
.git - 独立克隆(是包含相同远程仓库的目录)
.git - 无关目录(不同远程仓库或非Git仓库)—— 忽略此类目录
- 工作树(
- 列出父目录中匹配
-
收集所有分支:bash
git branch -vv --no-color
Phase 2 — Analysis
阶段2 — 分析
For each branch (excluding /), determine its status:
mainmaster-
Check remote tracking:
- If tracking branch shows → remote branch was deleted (PR likely merged or closed)
gone - If tracking branch exists → check PR status
- If tracking branch shows
-
Check GitHub PR status (ifis available):
ghbashgh pr list --state all --json number,title,headRefName,state- Map each branch to its PR (if any)
- Categorize: ,
merged,open(without merge), orclosedno PR
-
Check for uncommitted changes in each worktree:bash
git -C <worktree-path> status --short -
Check for stashes:bash
git stash list -
Check if main worktree is on— flag if it's on a feature branch
main
对每个分支(排除 /),确定其状态:
mainmaster-
检查远程跟踪状态:
- 如果跟踪分支显示 → 远程分支已删除(PR可能已合并或关闭)
gone - 如果跟踪分支存在 → 检查PR状态
- 如果跟踪分支显示
-
检查GitHub PR状态(如果可用):
ghbashgh pr list --state all --json number,title,headRefName,state- 将每个分支映射到对应的PR(如果存在)
- 分类:(已合并)、
merged(开放)、open(未合并关闭)或closed(无PR)no PR
-
检查每个工作树中的未提交更改:bash
git -C <worktree-path> status --short -
检查暂存内容:bash
git stash list -
检查主工作树是否处于分支 — 如果处于功能分支则标记
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:
-
Remove worktree directories:
- Try first
git worktree remove <path> - Before any fallback delete, resolve to a canonical path and verify all of the following:
<path>- 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 then
rm -rf <path>git worktree prune
- Try
-
Stash uncommitted changes (if any, on branches being kept):bash
git -C <worktree-path> stash push -m "reconcile: WIP on <branch-name>" -
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 -
Delete merged branches:bash
git branch -d <branch1> <branch2> ... -
Clear stale stashes (only if user confirmed):
- Drop stashes that reference deleted branches
-
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)
/
- Apply the same canonical path-safety checks before deletion (non-empty, exists, not
确认后,按以下顺序执行清理:
-
移除工作树目录:
- 首先尝试
git worktree remove <path> - 在执行任何备用删除操作前,将 解析为规范路径并验证以下所有条件:
<path>- 路径非空且存在
- 路径不是 或主目录
/ - 路径位于允许的前缀下(仓库同级工作树/克隆根目录)
- 路径不是超出允许前缀的符号链接
- 只有获得用户明确确认后,如果移除仍失败(孤立目录),才执行 然后
rm -rf <path>git worktree prune
- 首先尝试
-
暂存未提交更改(如果存在于需保留的分支):bash
git -C <worktree-path> stash push -m "reconcile: WIP on <branch-name>" -
将主工作树切换到默认分支:bash
DEFAULT_BRANCH=$(git symbolic-ref --short refs/remotes/origin/HEAD | sed 's#^origin/##') git checkout "$DEFAULT_BRANCH" git pull -
删除已合并分支:bash
git branch -d <branch1> <branch2> ... -
清理陈旧暂存内容(仅当用户确认):
- 删除引用已删除分支的暂存内容
-
移除独立克隆(仅当用户确认):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 or
mainbranchesmaster - 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 is not available, fall back to checking remote tracking status only
gh - 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且无远程分支的分支需标记供用户决策