GitHub CLI 使用指南
在 macOS 上使用 GitHub CLI(gh)非常方便,下面是从安装到使用的完整指南,并附上几个常用命令示例。
一、安装 GitHub CLI(macOS)
方法一:通过 Homebrew(推荐)
shell
brew install gh
安装完成后,可以验证版本:
shell
gh --version
二、登录你的 GitHub 账户
首次使用需要登录 GitHub 账户授权:
shell
gh auth login
你会看到提示:
text
What account do you want to log into? > GitHub.com
What is your preferred protocol for Git operations? > HTTPS
Authenticate Git with your GitHub credentials? > Yes
How would you like to authenticate GitHub CLI? > Login with a web browser
✅ 打开浏览器输入提示的代码,即可完成登录。
三、常用 GitHub CLI 示例命令
查看当前登录信息
shell
gh auth status
创建 Issue
shell
gh issue create \
--title "🔥 New issue from CLI" \
--body "This issue was created using GitHub CLI on macOS" \
--label "bug" \
--repo your-username/your-repo
🔎 参数说明:
参数 | 说明 |
---|---|
--title | Issue 标题 |
--body | Issue 内容 |
--label | 添加标签(可多个) |
--repo | 指定仓库(owner/repo 格式) |
实战经验
- label标签如果不存在会报错。
- repo中owner可以是组织名或用户名。
- 在CLI中创建的issue无法使用yml模板,可以用md模板。和AI说的完全相反。
列出 Issue
shell
gh issue list --repo your-username/your-repo
创建 Pull Request
shell
gh pr create \
--title "Add new feature" \
--body "This PR adds a new feature." \
--base main \
--head my-feature-branch \
--repo your-username/your-repo
退出登录
shell
gh auth logout
四、实用配置文件(可选)
你也可以设置一个默认仓库,在 .bashrc 或 .zshrc 中加入:
shell
export GH_REPO=your-username/your-repo
这样就不需要每次写 --repo 参数了。
🧪 示例:快速测试一条命令
shell
gh repo view cli/cli --web
↑ 这条命令会在浏览器打开 GitHub CLI 项目的首页。
结语
GitHub CLI 是 GitHub 官方维护的强大命令行工具,特别适合自动化流程、快速 issue/pr 管理、脚本集成等。