soft-delete-git

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Soft 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:

步骤:

  1. Check for uncommitted changes
    bash
    git status --porcelain
    If the working tree is dirty, stop and ask the user how to proceed (commit, stash, or discard) before switching branches.
  2. Get current branch name
    bash
    git branch --show-current
    If already on the default branch, there is no branch to delete — stop and ask the user which branch they meant.
  3. Determine the default branch
    bash
    git symbolic-ref --short refs/remotes/origin/HEAD
    This returns e.g.
    origin/main
    — the default branch is the part after
    origin/
    . If the ref is unset, fall back to
    main
    .
  4. Check out the default branch
    bash
    git checkout <default-branch>
  5. Pull latest changes
    bash
    git pull
  6. Soft delete the previous branch
    bash
    git branch -d <branch-name>
  7. If soft delete fails, check for a squash merge
    git branch -d
    fails 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:
    bash
    gh pr view <branch-name> --json state,mergedAt
    • If the PR state is
      MERGED
      , confirm with the user, then delete with
      git branch -D <branch-name>
      .
    • Otherwise, escalate to the user rather than force-deleting.
  1. 检查未提交更改
    bash
    git status --porcelain
    如果工作树存在未提交更改,请停止操作并询问用户如何处理(提交、暂存或丢弃),之后再切换分支。
  2. 获取当前分支名称
    bash
    git branch --show-current
    如果已处于默认分支,则没有可删除的分支——停止操作并询问用户指的是哪个分支。
  3. 确定默认分支
    bash
    git symbolic-ref --short refs/remotes/origin/HEAD
    该命令会返回例如
    origin/main
    ——默认分支是
    origin/
    之后的部分。如果引用未设置,则退回到
    main
    分支。
  4. 检出默认分支
    bash
    git checkout <default-branch>
  5. 拉取最新更改
    bash
    git pull
  6. 软删除之前的分支
    bash
    git branch -d <branch-name>
  7. 如果软删除失败,检查是否为 squash 合并
    对于通过GitHub“Squash and merge”(压缩合并)的分支,
    git branch -d
    会执行失败,因为分支尖端从未成为默认分支的祖先。请检查该分支的PR是否已实际合并:
    bash
    gh pr view <branch-name> --json state,mergedAt
    • 如果PR状态为
      MERGED
      ,请先与用户确认,然后执行
      git branch -D <branch-name>
      进行删除。
    • 否则,将操作交由用户确认,而非强制删除。

Notes:

注意事项:

  • Uses lowercase
    -d
    for soft delete (safe delete that prevents deletion of unmerged branches)
  • Never use uppercase
    -D
    (hard delete) except in the verified-squash-merge case above, and only after confirming with the user
  • The workflow ensures the default branch is up to date before attempting the delete
  • 使用小写
    -d
    进行软删除(安全删除,可防止删除未合并分支)
  • 除非上述已验证的压缩合并场景,否则绝不使用大写
    -D
    (强制删除),且仅在获得用户确认后使用
  • 工作流确保在尝试删除前默认分支已更新至最新状态