soft-delete-git
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSoft Delete Git Branch Workflow
Git分支软删除工作流
Invocation behavior
调用行为
When this skill is invoked without an explicit branch name, target the currently checked-out branch. Invocation of the skill is authorization to begin the workflow immediately: inspect the working tree and determine the current branch without asking which branch to delete.
Only ask the user how to proceed when:
- The working tree is dirty.
- The current branch is the default branch.
- Safe deletion fails and force deletion would require confirmation.
Do not ask the user to name or confirm the current branch before starting.
当未指定明确分支名称调用此技能时,目标为当前检出的分支。调用该技能即表示授权立即启动工作流:无需询问要删除哪个分支,直接检查工作树并确定当前分支。
仅在以下情况询问用户如何继续:
- 工作树存在未提交更改。
- 当前分支为默认分支。
- 安全删除失败,且强制删除需要确认。
开始前不要要求用户命名或确认当前分支。
Steps:
步骤:
-
Check for uncommitted changesbash
git status --porcelainIf the working tree is dirty, stop and ask the user how to proceed (commit, stash, or discard) before switching branches. -
Get current branch namebash
git branch --show-currentIf already on the default branch, there is no branch to delete — stop and ask the user which branch they meant. -
Determine the default branchbash
git symbolic-ref --short refs/remotes/origin/HEADThis returns e.g.— the default branch is the part afterorigin/main. If the ref is unset, fall back toorigin/.main -
Check out the default branchbash
git checkout <default-branch> -
Pull latest changesbash
git pull -
Soft delete the previous branchbash
git branch -d <branch-name> -
If soft delete fails, check for a squash mergefails for branches merged via GitHub's "Squash and merge", because the branch tip is never an ancestor of the default branch. Check whether the branch's PR was actually merged:
git branch -dbashgh pr view <branch-name> --json state,mergedAt- If the PR state is , confirm with the user, then delete with
MERGED.git branch -D <branch-name> - Otherwise, escalate to the user rather than force-deleting.
- If the PR state is
-
检查未提交更改bash
git status --porcelain如果工作树存在未提交更改,请停止操作并询问用户如何处理(提交、暂存或丢弃),之后再切换分支。 -
获取当前分支名称bash
git branch --show-current如果已处于默认分支,则没有可删除的分支——停止操作并询问用户指的是哪个分支。 -
确定默认分支bash
git symbolic-ref --short refs/remotes/origin/HEAD该命令会返回例如——默认分支是origin/main之后的部分。如果引用未设置,则退回到origin/分支。main -
检出默认分支bash
git checkout <default-branch> -
拉取最新更改bash
git pull -
软删除之前的分支bash
git branch -d <branch-name> -
如果软删除失败,检查是否为 squash 合并对于通过GitHub“Squash and merge”(压缩合并)的分支,会执行失败,因为分支尖端从未成为默认分支的祖先。请检查该分支的PR是否已实际合并:
git branch -dbashgh pr view <branch-name> --json state,mergedAt- 如果PR状态为,请先与用户确认,然后执行
MERGED进行删除。git branch -D <branch-name> - 否则,将操作交由用户确认,而非强制删除。
- 如果PR状态为
Notes:
注意事项:
- Uses lowercase for soft delete (safe delete that prevents deletion of unmerged branches)
-d - Never use uppercase (hard delete) except in the verified-squash-merge case above, and only after confirming with the user
-D - The workflow ensures the default branch is up to date before attempting the delete
- 使用小写进行软删除(安全删除,可防止删除未合并分支)
-d - 除非上述已验证的压缩合并场景,否则绝不使用大写(强制删除),且仅在获得用户确认后使用
-D - 工作流确保在尝试删除前默认分支已更新至最新状态