jackin-checkout-pr

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

jackin-checkout-pr

jackin-checkout-pr

Switch the working repo onto a PR's branch via
gh pr checkout
. Accepts a number, URL, or branch name.
通过
gh pr checkout
将当前工作仓库切换至PR分支。支持传入PR编号、URL或分支名称。

Process

流程

  1. Resolve to a PR number. Number or URL → pass to
    gh pr checkout
    directly. Branch name →
    gh pr list --head <branch> --state all --json number -q '.[0].number'
    ; no match → STOP, do not fall back to raw
    git checkout
    .
  2. Guard the working tree.
    git status --porcelain
    — if dirty, STOP and ask the operator to commit, stash, or discard first. Never auto-stash. Note
    git branch --show-current
    (the way back).
  3. Check the PR is open.
    gh pr view <N> --json state,headRefName
    .
    MERGED
    or
    CLOSED
    → warn (the branch may be stale) and confirm before proceeding.
  4. Switch.
    gh pr checkout <N>
    . Verify
    git branch --show-current
    matches
    headRefName
    .
  5. Report. Switched from
    <old>
    to
    <headRefName>
    (PR #N). Way back:
    git switch <old>
    .
  1. 解析为PR编号:若传入的是编号或URL,直接传递给
    gh pr checkout
    ;若为分支名称,则执行
    gh pr list --head <branch> --state all --json number -q '.[0].number'
    获取对应PR编号;若无匹配结果,则终止操作,不回退到直接执行
    git checkout
  2. 保护工作树:执行
    git status --porcelain
    检查工作树状态——若存在未提交修改,终止操作并提示操作者先提交、暂存或丢弃修改。绝不自动暂存。记录当前分支(
    git branch --show-current
    ),以便后续返回。
  3. 检查PR状态:执行
    gh pr view <N> --json state,headRefName
    查看PR状态。若PR为
    MERGED
    (已合并)或
    CLOSED
    (已关闭),则发出警告(分支可能已过时),并在继续操作前确认。
  4. 切换分支:执行
    gh pr checkout <N>
    。验证
    git branch --show-current
    的结果是否与
    headRefName
    一致。
  5. 反馈结果:提示已从
    <old>
    分支切换至
    <headRefName>
    分支(对应PR #N)。返回原分支的命令:
    git switch <old>