Files
Qiniu/technical/mac-dev-env-setup/gh-cli-commands.md
T

137 lines
3.4 KiB
Markdown
Raw Normal View History

2026-07-01 23:09:21 +08:00
---
tags: [github, cli, dev-env]
create time: 2026-07-01 00:00
---
# GitHub CLI (gh) 命令速查
## 概述
`gh` 是 GitHub 官方 CLI,在终端完成 PR、Issue、Fork、Clone 等操作,比浏览器更流畅。它与 Git + Claude Code 组合可以形成完整的高效开发工作流。
## 安装
```bash
# macOS
brew install gh
# 验证
gh --version
gh auth login # 首次运行,按提示登录 GitHub 账号
```
## Pull Request 操作
### 查看与筛选
```bash
gh pr list # 当前仓库的 open PR(最近 30 条)
gh pr list --state closed # 已关闭的 PR
gh pr list --assignee your-user # 分配给自己的 PR
gh pr status # 自己相关的 PR:当前分支 / 创建过 / 需要你 review
```
### Checkout 到本地
```bash
gh pr checkout 12 # 通过 PR 编号 checkout(自动处理 fork)
gh pr checkout patch-2 # 通过分支名 checkout
gh pr view 12 # 终端内查看 PR 详情
gh pr view 12 --web # 浏览器打开 PR
```
### 创建 PR
```bash
gh pr create # 交互式创建
gh pr create --title "feat: add xxx" --body "description"
gh pr create --web # 浏览器打开创建页
```
## Issue 操作
```bash
gh issue list # 列出 open issues
gh issue list --label bug # 按标签过滤
gh issue list --assignee user # 按负责人过滤
gh issue status # 与你相关的 issues(分配/提及/自己开的)
gh issue view 42 # 终端查看 issue 详情
gh issue view 42 --web # 浏览器打开
gh issue create # 交互式创建
gh issue create --label bug --title "fix: xxx"
```
## Repository 操作
### Clone
```bash
gh repo clone owner/repo # OWNER/REPO 语法
gh repo clone https://github.com/owner/repo # URL 语法
```
### Fork
```bash
# 在已有仓库目录中
gh repo fork # 自动 fork + 询问是否添加 remote
# 跳过 remote 提示
gh repo fork --remote=false
# Fork 后跳过克隆
gh repo fork owner/repo --clone=false
# Fork 时直接克隆
gh repo fork owner/repo # 会自动问是否 clone
```
### View
```bash
gh repo view owner/repo # 终端查看仓库信息 + README
gh repo view owner/repo --web # 浏览器打开
gh repo view # 当前所在仓库
```
## 常见工作流示例
### Fork → 开发 → 提 PR
```bash
gh repo fork owner/repo # 1. Fork
cd repo # 进入目录
git checkout -b feature/new-ui # 2. 创建功能分支
# ... 编码 ...
git commit -m "feat: new ui"
git push origin feature/new-ui # 3. 推送
gh pr create --title "feat: new UI components" \
--body "Closes #42" # 4. 创建 PR
```
### Review 别人提交的 PR
```bash
gh pr checkout 8896 # 1. Checkout 别人的 PR
git pull origin patch-2 # 确保最新
# ... 审查代码 ...
```
### Claude Code + gh 协作
```bash
gh issue list # 1. 查看任务列表
gh issue view 42 # 查看具体需求
claude # 2. AI 助手直接在当前分支实现
```
## 参考链接
- GitHub 官方文档: <https://cli.github.com/>
- [[mac-dev-env-setup]] — macOS 开发环境配置总指南
- [[mac-dev-env-setup/ai-coding-agent]] — AI Coding Agent 安全边界与提示策略