别点 Compare,第一条 PR 已在终端

第一条 PR 的证据是 gh 打印的 URL,不是浏览器里的 Compare 按钮。

分支已经推上去,还要打开 GitHub 点 Compare 才算开 PR?不必。 gh pr create 成功时会把 pull 地址打到终端——那就是第一条 PR。需要一起摊图时再 即刻开会。

本文只做到这一步:仓库已有 GitHub remote,你有推送或 fork 权限,命令打印 URL 后停手。检索词是 gh 安装、auth login、终端开出第一条 PR

ghauth logingh pr create
gh pr createURL on stdout
--webopens the tab
cli.github.commanual

先让 gh 出现在 PATH

  1. 1
    macOS

    Homebrew:brew install gh。然后 gh --version。

  2. 2
    Windows

    winget install --id GitHub.cli。新开终端再测版本。

  3. 3
    Linux

    按 cli.github.com 加入官方 apt/dnf 源后安装 gh。不要用过期的发行版包当唯一来源。

  4. 4
    确认命令

    which gh 有路径,gh --version 有版本号。没有就修 PATH。

gh auth login 只解决身份

默认是浏览器 OAuth。hostname 默认 github.com。企业实例加 --hostname。无图形环境可用 --with-token 从标准输入读 classic PAT(手册要求至少 repo、read:org、gist)。

gh auth login
gh auth status
# headless classic PAT:
# gh auth login --with-token < mytoken.txt

把第一条 PR 开在终端里

先换分支、提交、让远程有这条 head。分支未推送时,gh pr create 会问推到哪里。用 --title 和 --body 跳过编辑器。成功行是一条 https://github.com/…/pull/N。

git checkout -b fix-docs
git add .
git commit -m "Fix docs"
git push -u origin fix-docs
gh pr create --title "Fix docs" --body "What changed and why."
# printed: https://github.com/owner/repo/pull/1
gh pr view

会卡住的只有权限,不是标签页

没有推送权会走 fork

手册:当前分支没完全推到 remote 时会提示推送;对无权推的上游,gh 可以创建只有默认分支的 fork。用 --head 可跳过自动 fork/push。

令牌不要进仓库

PAT 从文件或环境读。不要写进 commit、justfile 或聊天记录。退出:gh auth logout。

Compare 按钮是网页捷径。gh 打印的 /pull/N 才是 PR。

PR 描述要和评审图对上,可以摊到同一页: 使用指南 · hereink 开房

一定要打开 github.com 吗?

开 PR 不必。gh pr create 成功会打印 URL。--web 才会打开创建页。auth login 的默认流程会用浏览器完成 OAuth,那是登录,不是创建 PR。

和已有的 git clone 冲突吗?

不冲突。remote 指向 GitHub 即可。不必用 gh repo clone。

为什么命令报没有仓库?

要在 Git 仓库里跑,且 origin 是 GitHub。可用 -R owner/repo。

草稿怎么开?

加 --draft。指定目标分支用 --base。查看用 gh pr view,不要默认加 --web。

即刻开会