pr-anywhere

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PR Anywhere

PR Anywhere

Make a change to any GitHub repository and open a pull request, from any terminal. Always works in the ghq-managed clone so behaviour is identical everywhere and never disturbs whatever checkout you happen to be sitting in.
无需在本地检出仓库,即可从任意终端修改任意GitHub仓库并创建拉取请求(PR)。 始终在ghq管理的克隆仓库中操作,确保在所有终端的行为一致,且绝不会干扰当前终端中已检出的任何仓库。

Steps (follow strictly)

步骤(严格遵循)

1. Parse the arguments

1. 解析参数

The first token of
args
is the target repository when it looks like
owner/repo
(or a full GitHub URL). Everything after it is the change to make.
  • No
    owner/repo
    first token: ask the user which repository to change. Do not guess from the current directory — this skill is meant to be run from anywhere, so the current checkout says nothing about the intended target.
  • Repository given but no change described: ask what to change.
args
的第一个参数格式为
owner/repo
(或完整GitHub URL)时,该参数即为目标仓库,后续所有内容为需要执行的修改。
  • 若第一个参数不是
    owner/repo
    格式:询问用户要修改哪个仓库。请勿从当前目录猜测——此工具旨在从任意位置运行,因此当前检出的仓库与目标仓库无关。
  • 已指定仓库但未描述修改内容:询问用户具体要进行哪些修改。

2. Preflight: gh auth and ghq

2. 预检:gh 身份验证与 ghq

bash
gh auth status
If
gh auth status
fails, STOP and tell the user to run
! gh auth login
, then wait.
bash
command -v ghq || mise use -g ghq@latest
If
mise use -g ghq@latest
fails to resolve, fall back to
mise use -g ubi:x-motemen/ghq
.
bash
gh auth status
如果
gh auth status
执行失败,立即停止操作并告知用户运行
! gh auth login
,等待用户完成后再继续。
bash
command -v ghq || mise use -g ghq@latest
如果
mise use -g ghq@latest
无法解析, fallback 到
mise use -g ubi:x-motemen/ghq

3. Resolve the repository's facts

3. 获取仓库信息

Never assume the default branch is
main
, and never assume you can push to it.
bash
gh repo view <owner/repo> --json defaultBranchRef,viewerPermission
  • defaultBranchRef.name
    — the base branch for the PR.
  • viewerPermission
    ADMIN
    ,
    MAINTAIN
    , or
    WRITE
    means you can push a branch directly to origin.
    TRIAGE
    ,
    READ
    , or
    NONE
    means you must fork (step 7).
If the command fails, the repo does not exist or is not visible to this account. Report that and stop.
切勿默认分支是
main
,也不要假设你拥有推送权限。
bash
gh repo view <owner/repo> --json defaultBranchRef,viewerPermission
  • defaultBranchRef.name
    — PR的基准分支。
  • viewerPermission
    — 若为
    ADMIN
    MAINTAIN
    WRITE
    ,则可直接向源仓库推送分支;若为
    TRIAGE
    READ
    NONE
    ,则必须先fork仓库(步骤7)。
如果该命令执行失败,说明仓库不存在或当前账号无访问权限。请告知用户并停止操作。

4. Get (clone or update) the repository

4. 获取(克隆或更新)仓库

bash
ghq get -u github.com/<owner>/<repo>
repo="$(ghq list --full-path --exact github.com/<owner>/<repo>)"
cd "$repo"
bash
ghq get -u github.com/<owner>/<repo>
repo="$(ghq list --full-path --exact github.com/<owner>/<repo>)"
cd "$repo"

5. Sync the base branch and create a working branch

5. 同步基准分支并创建工作分支

Always branch off the fresh default branch — never edit it directly, never push to it.
bash
git switch <default-branch>
git pull --ff-only
git switch -c "<type>/<slug>"
  • <type>
    :
    feat
    /
    fix
    /
    refactor
    /
    docs
    /
    chore
    (match the change)
  • <slug>
    : short kebab-case summary, e.g.
    feat/add-ripgrep
    ,
    fix/install-typo
If the branch already exists from a prior run, pick a new slug (append
-2
, etc.).
始终基于最新的默认分支创建新分支——绝不要直接编辑默认分支,也不要向其推送内容。
bash
git switch <default-branch>
git pull --ff-only
git switch -c "<type>/<slug>"
  • <type>
    :
    feat
    /
    fix
    /
    refactor
    /
    docs
    /
    chore
    (根据修改类型选择)
  • <slug>
    : 简短的连字符分隔式摘要,例如
    feat/add-ripgrep
    fix/install-typo
如果该分支在之前的操作中已存在,请使用新的slug(例如追加
-2
等)。

6. Learn the repo's conventions, then apply the edit

6. 了解仓库规范,然后执行修改

Before editing, read whatever the repository says about itself —
CLAUDE.md
,
AGENTS.md
,
CONTRIBUTING.md
,
.github/PULL_REQUEST_TEMPLATE.md
— and check
git log --oneline -20
for the commit message style actually in use. Follow them over any habit of your own.
Make the requested change with the normal edit tools. Keep it focused — one logical change per PR.
Run whatever check the repo defines (lint, test, typecheck) if one is obvious from its config or CI workflow. Report failures rather than working around them.
在修改前,请阅读仓库中的相关文档——如
CLAUDE.md
AGENTS.md
CONTRIBUTING.md
.github/PULL_REQUEST_TEMPLATE.md
——并查看
git log --oneline -20
了解仓库实际使用的提交信息格式。请严格遵循仓库规范,而非个人习惯。
使用常规编辑工具完成请求的修改。保持修改聚焦——每个PR仅包含一个逻辑修改。
如果从仓库配置或CI工作流中可明显看出需要执行检查(如lint、测试、类型检查),请运行相应检查。若检查失败,需告知用户而非自行绕过。

7. Push

7. 推送分支

With write access (step 3 said
ADMIN
/
MAINTAIN
/
WRITE
):
bash
git push -u origin "<branch>"
Without write access — fork first, and push the branch to the fork:
bash
gh repo fork --remote --remote-name fork
git push -u fork "<branch>"
拥有写入权限(步骤3中权限为
ADMIN
/
MAINTAIN
/
WRITE
):
bash
git push -u origin "<branch>"
无写入权限 — 先fork仓库,再将分支推送到fork的仓库:
bash
gh repo fork --remote --remote-name fork
git push -u fork "<branch>"

8. Open the PR

8. 创建PR

bash
gh pr create --base <default-branch> --title "<type>: <summary>" --body "<why>"
Add
--repo <owner/repo>
when pushing from a fork, so the PR lands on the upstream repository rather than the fork.
Write the body yourself — state what changed and why. Use
--fill
only when the commit message already says everything the reviewer needs.
bash
gh pr create --base <default-branch> --title "<type>: <summary>" --body "<why>"
如果是从fork的仓库推送分支,请添加
--repo <owner/repo>
参数,确保PR提交到上游仓库而非fork的仓库。
请自行编写PR正文——说明修改内容及原因。仅当提交信息已包含评审所需的全部内容时,才可使用
--fill
参数。

9. Report

9. 反馈结果

Output the PR URL returned by
gh pr create
.
输出
gh pr create
返回的PR链接。

Gotchas

注意事项

  • Never touch the current checkout: even when invoked from inside a checkout of the target repo, work in the ghq clone. This keeps behaviour identical on every terminal and avoids disturbing uncommitted work.
  • The default branch is not always
    main
    : resolve it in step 3. Hardcoding
    main
    fails on
    master
    repos and on any repo using a release branch as default.
  • gh not authenticated: step 8 fails cryptically. Verify with
    gh auth status
    in step 2 first.
  • command -v ghq
    is checked by exit code
    :
    || mise use ...
    only installs when truly missing.
  • git pull --ff-only
    : fails loudly if the local clone diverged from origin (e.g. leftover commits on the default branch). If it fails,
    git reset --hard origin/<default-branch>
    after confirming there is nothing to keep.
  • Branch already exists:
    git switch -c
    fails. Reuse it (
    git switch <branch>
    ) only if it is yours and clean, otherwise choose a new slug.
  • A stale ghq clone of a fork: if
    ghq get
    lands on a clone whose
    origin
    is your fork rather than upstream,
    gh repo view
    still describes upstream while
    git push origin
    goes to the fork. Check
    git remote -v
    when the PR base looks wrong.
  • 绝不触碰当前检出的仓库:即使在目标仓库的检出目录中调用此工具,也需在ghq克隆的仓库中操作。这样可确保在所有终端的行为一致,并避免干扰未提交的工作。
  • 默认分支不一定是
    main
    :请在步骤3中确认默认分支。硬编码
    main
    会导致在使用
    master
    或其他发布分支作为默认分支的仓库上操作失败。
  • gh未完成身份验证:步骤8会出现模糊的失败信息。请在步骤2中先通过
    gh auth status
    验证身份。
  • 通过退出码检查
    command -v ghq
    || mise use ...
    仅在ghq确实缺失时才会安装。
  • git pull --ff-only
    :如果本地克隆与源仓库出现分歧(例如默认分支上存在遗留提交),该命令会直接失败。若失败,在确认无需要保留的内容后,执行
    git reset --hard origin/<default-branch>
  • 分支已存在
    git switch -c
    会失败。仅当分支属于你且状态干净时才可复用(
    git switch <branch>
    ),否则请选择新的slug。
  • ghq克隆的fork仓库已过期:如果
    ghq get
    获取的克隆仓库的
    origin
    是你的fork而非上游仓库,
    gh repo view
    仍会显示上游仓库信息,但
    git push origin
    会推送到fork的仓库。当PR基准分支看起来异常时,请检查
    git remote -v