gh-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitHub CLI Quick Reference

GitHub CLI 快速参考

The
gh
CLI is GitHub's official command-line tool. This is a quick reference for common workflows—for comprehensive docs, see https://cli.github.com/manual
gh
CLI是GitHub官方的命令行工具。本手册是常见工作流的快速参考——如需完整文档,请访问https://cli.github.com/manual

Getting Help

获取帮助

bash
gh --help                    # List all commands
gh <command> --help          # Help for specific command
gh auth status               # Check authentication
bash
gh --help                    # 列出所有命令
gh <command> --help          # 查看特定命令的帮助
gh auth status               # 检查认证状态

Discovery Patterns

发现模式

bash
gh <command> --web           # Open in browser
gh <command> --json FIELDS   # JSON output for scripting
gh <command> <subcommand> -h # Quick help for any command
gh <command> list --limit N  # Limit results to avoid large output (default: 20-30)
Use tab completion to explore available commands and flags.
Important: Always use
--limit
when querying lists to avoid overwhelming output, especially with
pr list
,
issue list
,
run list
, etc.
bash
gh <command> --web           # 在浏览器中打开
gh <command> --json FIELDS   # 输出JSON格式用于脚本编写
gh <command> <subcommand> -h # 查看任意命令的快速帮助
gh <command> list --limit N  # 限制结果数量以避免输出过多(默认:20-30条)
使用标签补全功能探索可用的命令和参数。
重要提示: 查询列表时请始终使用
--limit
参数,避免输出内容过多,尤其是使用
pr list
issue list
run list
等命令时。

Common Workflows

常见工作流

PR Workflow

PR工作流

bash
undefined
bash
undefined

Create PR

创建PR

gh pr create --fill # Use commit messages for title/body gh pr create --web # Open browser to create PR
gh pr create --fill # 使用提交信息作为标题/正文 gh pr create --web # 打开浏览器创建PR

View and checkout

查看并检出PR

gh pr list # List PRs gh pr view [NUMBER] # View PR details gh pr checkout NUMBER # Checkout PR locally
gh pr list # 列出PR gh pr view [NUMBER] # 查看PR详情 gh pr checkout NUMBER # 在本地检出PR

Review

评审PR

gh pr review NUMBER --approve gh pr review NUMBER --comment -b "feedback"
gh pr review NUMBER --approve gh pr review NUMBER --comment -b "反馈内容"

Merge

合并PR

gh pr merge --squash --delete-branch
undefined
gh pr merge --squash --delete-branch
undefined

Review Workflow

评审工作流

bash
undefined
bash
undefined

Find PRs needing your review

查找需要你评审的PR

gh pr list --search "review-requested:@me"
gh pr list --search "review-requested:@me"

Review process

评审流程

gh pr checkout NUMBER
gh pr checkout NUMBER

... test locally ...

... 本地测试 ...

gh pr review NUMBER --approve
undefined
gh pr review NUMBER --approve
undefined

CI/CD Debugging

CI/CD 调试

bash
undefined
bash
undefined

Check recent runs

查看最近的运行记录

gh run list --limit 5 gh run list --status failure
gh run list --limit 5 gh run list --status failure

View logs

查看日志

gh run view RUN_ID --log-failed
gh run view RUN_ID --log-failed

Rerun after fix

修复后重新运行

gh run rerun RUN_ID --failed
undefined
gh run rerun RUN_ID --failed
undefined

Issue Triage

Issue 分类处理

bash
gh issue list
gh issue list --assignee @me
gh issue create --title "Title" --body "Description"
gh issue view NUMBER
gh issue comment NUMBER -b "Comment"
gh issue close NUMBER
bash
gh issue list
gh issue list --assignee @me
gh issue create --title "标题" --body "描述内容"
gh issue view NUMBER
gh issue comment NUMBER -b "评论内容"
gh issue close NUMBER

Core Commands Quick Reference

核心命令快速参考

Pull Requests

拉取请求(PR)

bash
gh pr list [--state open|closed|merged] [--author @me]
gh pr create [--draft] [--title "..."] [--body "..."]
gh pr view [NUMBER] [--web]
gh pr checkout NUMBER
gh pr diff [NUMBER]
gh pr merge [NUMBER] [--squash|--merge|--rebase]
bash
gh pr list [--state open|closed|merged] [--author @me]
gh pr create [--draft] [--title "..."] [--body "..."]
gh pr view [NUMBER] [--web]
gh pr checkout NUMBER
gh pr diff [NUMBER]
gh pr merge [NUMBER] [--squash|--merge|--rebase]

Issues

Issue

bash
gh issue list [--assignee @me] [--label "bug"]
gh issue create [--title "..."] [--body "..."]
gh issue view NUMBER [--web]
gh issue close NUMBER
bash
gh issue list [--assignee @me] [--label "bug"]
gh issue create [--title "..."] [--body "..."]
gh issue view NUMBER [--web]
gh issue close NUMBER

Workflows & Runs

工作流与运行记录

bash
gh run list [--workflow "CI"] [--status failure]
gh run view RUN_ID [--log] [--log-failed]
gh run watch RUN_ID
gh workflow run WORKFLOW_FILE [--ref branch]
bash
gh run list [--workflow "CI"] [--status failure]
gh run view RUN_ID [--log] [--log-failed]
gh run watch RUN_ID
gh workflow run WORKFLOW_FILE [--ref branch]

Repositories

仓库

bash
gh repo clone OWNER/REPO
gh repo view [--web]
gh repo fork OWNER/REPO
gh repo create NAME [--public|--private]
bash
gh repo clone OWNER/REPO
gh repo view [--web]
gh repo fork OWNER/REPO
gh repo create NAME [--public|--private]

Power User Tips

高级用户技巧

JSON Output

JSON 输出

bash
undefined
bash
undefined

Get structured data

获取结构化数据

gh pr list --json number,title,author
gh pr list --json number,title,author

Filter with jq

使用 jq 过滤

gh pr list --json number,title | jq '.[] | select(.number > 100)'
undefined
gh pr list --json number,title | jq '.[] | select(.number > 100)'
undefined

API Access

API 访问

bash
undefined
bash
undefined

Direct API calls

直接调用API

gh api repos/OWNER/REPO gh api repos/OWNER/REPO/pulls -f title="PR Title" -f head=branch -f base=main
gh api repos/OWNER/REPO gh api repos/OWNER/REPO/pulls -f title="PR标题" -f head=branch -f base=main

GraphQL

GraphQL 查询

gh api graphql -f query='{ viewer { login } }'
undefined
gh api graphql -f query='{ viewer { login } }'
undefined

Aliases

别名

bash
gh alias set pv 'pr view'
gh alias set co 'pr checkout'
gh alias list
bash
gh alias set pv 'pr view'
gh alias set co 'pr checkout'
gh alias list

Environment Variables

环境变量

  • GH_TOKEN
    : Authentication token
  • GH_REPO
    : Default repository (OWNER/REPO format)
  • GH_EDITOR
    : Preferred editor for interactive commands
  • GH_PAGER
    : Pager for output (e.g.,
    less
    )
  • GH_TOKEN
    : 认证令牌
  • GH_REPO
    : 默认仓库(格式为OWNER/REPO)
  • GH_EDITOR
    : 交互式命令首选的编辑器
  • GH_PAGER
    : 输出分页工具(例如
    less

Finding Your Work

查找你的工作内容

bash
gh pr list --author @me
gh issue list --assignee @me
gh search prs "author:username is:open"
gh search issues "assignee:username is:open"
bash
gh pr list --author @me
gh issue list --assignee @me
gh search prs "author:username is:open"
gh search issues "assignee:username is:open"