如何安装 GitHub CLI(gh):auth login 登录、clone 仓库与创建 Issue 和 Pull Request
安装 gh,完成 auth login,clone 仓库,并从命令行创建 Issue 与 PR。
在浏览器里点 GitHub 很快,但GitHub CLI(命令 gh)让你在终端完成登录、克隆仓库、开 Issue 和提 Pull Request,适合脚本与日常开发流。本教程覆盖 macOS/Linux/Windows 安装、gh auth login、gh repo clone、gh issue create 与 gh pr create,面向 hereink 读者常用的 Git 与协作工作流。
检索词是如何安装 GitHub CLI gh 并登录克隆仓库创建 Issue 和 PR。读完应能安装 gh、完成 GitHub.com 认证、克隆一个仓库,并从命令行创建 Issue 与 Pull Request。
gh 解决什么问题
终端里的 GitHub 操作
gh repo clone owner/repo、gh issue list、gh pr create 与 git 同屏完成,少切换浏览器标签。CI 与本地脚本也可调用同一套 CLI。
与 git 互补
git 管提交与分支;gh 管 GitHub 元数据(PR 描述、Review 请求、Release)。clone 后仍用 git push 推代码,PR 用 gh 创建更省事。
上游:
github.com/cli/cli,文档cli.github.com。安装后运行gh --version确认。
安装 GitHub CLI
- 1macOS(Homebrew)
brew install gh。验证:which gh与gh --version。 - 2Linux
Debian/Ubuntu:按
cli.github.com添加官方 apt 源后sudo apt install gh。Fedora:sudo dnf install gh。通用 tarball 见官方 Releases。 - 3Windows
winget install GitHub.cli或choco install gh。新开 PowerShell 后gh --version。 - 4检查 PATH
若命令找不到,确认安装目录在
PATH中;WSL 与 Windows 各装一份时,在你要用的 shell 里验证。
gh auth login 登录 GitHub
首次使用前必须认证。在终端运行 gh auth login,按提示选择:
gh auth login
# 典型选择(GitHub.com 个人账号):
# What account → GitHub.com
# Preferred protocol → HTTPS 或 SSH(与现有 git remote 一致即可)
# Authenticate → Login with a web browser(推荐)或 Paste an authentication token
# 浏览器完成 OAuth 后终端显示 ✓ Logged in验证:gh auth status 应显示已登录用户与 token 作用域。企业 GitHub Enterprise Server 需选对应 hostname,见官方 gh auth login 文档。
gh repo clone 克隆仓库
# HTTPS 或 SSH 由 auth 与 gh 配置决定
gh repo clone cli/cli
cd cli
# 指定目录名
gh repo clone owner/repo my-folder
# 浅克隆(大仓库)
gh repo clone owner/repo -- --depth=1等价于 git clone,但可省略完整 URL:gh repo clone owner/repo 自动解析。私有仓库需已 gh auth login 且账号有读权限。
gh issue create 创建 Issue
cd your-repo # 必须在 Git 仓库根或子目录,且 remote 指向 GitHub
gh issue create --title "Fix login timeout" --body "Steps to reproduce…"
# 交互式(编辑器填 body)
gh issue create
# 加标签与指派人
gh issue create --title "Docs: gh guide" --label documentation --assignee @me列出与查看:gh issue list、gh issue view 123。在 fork 上操作时,确认 git remote -v 的默认仓库是你要开 Issue 的目标 repo。
gh pr create 创建 Pull Request
- 1分支与推送
git checkout -b fix-docs,改代码后git commit,git push -u origin fix-docs(首次推送需 -u)。 - 2创建 PR
gh pr create --title "Fix docs" --body "Summary of changes",或裸gh pr create走交互/编辑器。 - 3常用选项
--draft草稿;--reviewer user1,user2;--base main指定目标分支。查看:gh pr list、gh pr view、gh pr checkout 42。
gh 与纯 git 对照
| 目标 | gh | git / 浏览器 |
|---|---|---|
| 克隆 | gh repo clone owner/repo | git clone https://github.com/owner/repo.git |
| 开 Issue | gh issue create | GitHub Web → Issues → New |
| 开 PR | gh pr create | Push 分支后在 Web 点 Compare & pull request |
| 看谁已登录 | gh auth status | 浏览器账号菜单 |
- 适合
- 日常开发、开源贡献、自动化脚本、与
git、rg、HTTPie 同一终端栈。 - 不适合
- 仅本地 Git、无 GitHub remote 的仓库——装 gh 也无 API 可连;复杂 Code Review UI 仍常回浏览器。
gh auth login 失败或浏览器打不开?
可选「Paste an authentication token」并粘贴具有 repo 等所需 scope 的 PAT。检查代理与防火墙;企业环境可能需要 SSO 授权 token。见 cli.github.com 的 troubleshooting。
必须用 gh clone 吗?
不必。已有 git clone 的仓库只要 remote 在 GitHub 上,gh issue / gh pr 仍可用。gh clone 只是更短的写法。
gh 和 GitHub Desktop 怎么选?
Desktop 偏图形化分支与 diff;gh 偏终端与脚本。两者可并存,认证独立配置。
PR 描述清楚后画一张图
架构或评审要点适合摊在同一张白板上。见 如何创建空间。