網頁沒開,第一條 PR 已經在
第一條 PR 的證據是 gh 印出的 URL,不是瀏覽器裡的 Compare。
分支已經推上去,還要打開 GitHub 點 Compare 才算開 PR?不必。 gh pr create 成功時會把 pull 網址打到終端——那就是第一條 PR。需要一起攤圖時再 立即開會。
本文只做到這一步:倉庫已有 GitHub remote,你有推送或 fork 權限,命令印出 URL 後停手。檢索詞是 安裝 gh、auth login、在終端開出第一條 PR
先讓 gh 出現在 PATH
- 1macOS
Homebrew:
brew install gh。然後gh --version。 - 2Windows
winget install --id GitHub.cli。新開終端再測版本。 - 3Linux
依 cli.github.com 加入官方 apt/dnf 源後安裝
gh。不要只用過期的發行版套件。 - 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。登出: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 即可。
為什麼命令報沒有倉庫?
要在 Git 倉庫裡跑,且 origin 是 GitHub。可用 -R owner/repo。
草稿怎麼開?
加 --draft。指定目標分支用 --base。查看用 gh pr view。