worktree-handoff
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWorktree Handoff
Worktree 变更移交
Mission
目标
Move all uncommitted changes from the current worktree onto a local target branch. If the user does not name a target branch, use .
develop将当前工作区中所有未提交的变更迁移到本地目标分支。若用户未指定目标分支,则默认使用分支。
developWhen To Use
使用场景
- User wants current worktree changes moved onto another local branch.
- Untracked files must come along with tracked edits.
- The repo uses multiple worktrees and the target branch may already have one.
- 用户希望将当前工作区的变更迁移到另一个本地分支。
- 需要将未跟踪文件与已跟踪文件的修改一同迁移。
- 仓库使用多个工作区,且目标分支可能已关联一个工作区。
Non-Negotiables
硬性要求
- Default target branch: .
develop - Include untracked files with .
git stash push -u - Do not use destructive git commands.
- Prefer the target branch's existing worktree if one already exists.
- Existing local changes on the target branch are allowed; apply on top and let Git merge.
- After applying, always inspect for conflicts and report them before resolving anything.
- If a temporary target worktree is created, report its path and leave it in place unless the user asks for cleanup.
- 默认目标分支:。
develop - 使用命令包含未跟踪文件。
git stash push -u - 禁止使用具有破坏性的Git命令。
- 若目标分支已存在关联的工作区,优先使用该工作区。
- 允许目标分支存在本地变更;将新变更叠加在其上,由Git进行合并。
- 应用变更后,必须先检查冲突并告知用户,再进行任何解决操作。
- 若创建了临时目标工作区,需告知其路径,且除非用户要求清理,否则保留该工作区。
Fast Resilient Workflow
快速可靠的工作流程
1) Preflight
1) 预检
bash
git status --short --branch
git worktree list --porcelain- replaces separate branch and status checks.
git status --short --branch - If is detached, use the short commit SHA as the source ref in the stash message.
HEAD
bash
git status --short --branch
git worktree list --porcelain- 使用替代单独的分支检查和状态检查。
git status --short --branch - 若处于分离状态,在暂存消息中使用简短的提交SHA作为源引用。
HEAD
2) Stash the source worktree, including untracked files
2) 暂存源工作区(包括未跟踪文件)
bash
git stash push -u -m "worktree-handoff: <source-ref> -> <target-branch> <timestamp>"- Use , not
-u, unless the user explicitly wants ignored files too.--all
bash
git stash push -u -m "worktree-handoff: <source-ref> -> <target-branch> <timestamp>"- 使用参数而非
-u,除非用户明确表示需要包含被忽略的文件。--all
3) Resolve the target worktree
3) 确定目标工作区
- If already shows
git worktree list --porcelain, use that worktree path.branch refs/heads/<target-branch> - Otherwise, create a sibling temporary worktree for the target branch instead of forcing a branch switch in the current worktree.
bash
git worktree add "../<repo>-handoff-<target-branch>" "<target-branch>"- If the target branch does not exist locally, stop and ask whether it should be created from or another base.
origin/<target-branch>
- 若输出中已显示
git worktree list --porcelain,则使用该工作区路径。branch refs/heads/<target-branch> - 否则,为目标分支创建同级临时工作区,而非强制在当前工作区切换分支。
bash
git worktree add "../<repo>-handoff-<target-branch>" "<target-branch>"- 若目标分支在本地不存在,需暂停操作并询问用户是否要从或其他基准创建该分支。
origin/<target-branch>
4) Check the target before applying
4) 应用前检查目标工作区
bash
git -C "<target-worktree>" status --short- Existing changes are fine.
- If both source and target already show the same path as added or untracked, report that path collision immediately instead of trying .
stash pop - If the target already has the same path staged as a new file or as an untracked file, Git cannot restore the stashed untracked file over it.
bash
git -C "<target-worktree>" status --short- 目标工作区存在现有变更是允许的。
- 若源工作区和目标工作区中存在相同路径的已添加或未跟踪文件,需立即报告路径冲突,而非尝试执行。
stash pop - 若目标工作区中已存在相同路径的暂存新文件或未跟踪文件,Git无法将暂存的未跟踪文件恢复到该路径上。
5) Apply the handoff onto the target branch
5) 将变更移交到目标分支
bash
git -C "<target-worktree>" stash pop --index- Prefer : on success the stash is removed; on conflict Git keeps the stash entry.
pop - Retry once without only for index incompatibility errors.
--index - If the failure says or
already exists, no checkout, do not retry; report the path collision.could not restore untracked files from stash
bash
git -C "<target-worktree>" stash pop --index- 优先使用命令:成功执行后会移除暂存记录;若发生冲突,Git会保留暂存条目。
pop - 仅当出现索引不兼容错误时,才尝试不带参数重新执行一次。
--index - 若失败提示为或
already exists, no checkout,请勿重试,直接报告路径冲突。could not restore untracked files from stash
6) Verify the result
6) 验证结果
bash
git -C "<target-worktree>" diff --name-only --diff-filter=U
git -C "<target-worktree>" status --shortbash
git -C "<target-worktree>" diff --name-only --diff-filter=U
git -C "<target-worktree>" status --shortReporting Rules
报告规则
- If the apply succeeds cleanly, report:
- source ref/worktree
- target branch/worktree
- whether the target already had local changes
- whether worked or a fallback was needed
--index - whether the stash still exists
- If there are conflicts, stop after applying and report:
- conflicted files
- whether the target branch already had overlapping edits
- the most likely resolution approach
Even if a conflict looks trivial, do not silently resolve it. Ask how to proceed after summarizing the conflict.
- 若变更应用成功且无冲突,需报告:
- 源引用/工作区
- 目标分支/工作区
- 目标分支是否已存在本地变更
- 参数是否生效,或是否需要使用备选方案
--index - 暂存记录是否仍存在
- 若存在冲突,应用后需暂停操作并报告:
- 冲突文件
- 目标分支是否已存在重叠修改
- 最可能的冲突解决方式
即使冲突看似轻微,也请勿自行静默解决。在总结冲突情况后,询问用户如何继续操作。