- 掌握 GitHub CLI 的高级功能
- 学会使用
gh管理仓库、Issue、PR - 了解
gh的扩展和别名
确保已安装 GitHub CLI 并登录:
# 安装(macOS)
brew install gh
# 安装(Windows)
winget install GitHub.cli
# 安装(Linux)
sudo apt install gh
# 登录
gh auth login# 创建仓库
gh repo create my-project --public --description "我的项目"
# 克隆仓库
gh repo clone owner/repo
# 列出仓库
gh repo list
# 查看仓库信息
gh repo view owner/repo
# 设置仓库默认分支
gh repo edit owner/repo --default-branch main# 创建 Issue
gh issue create --title "功能请求" --body "描述你的需求"
# 从模板创建
gh issue create --template "bug_report.md"
# 列出 Issue
gh issue list
# 查看 Issue
gh issue view 123
# 添加评论
gh issue comment 123 --body "评论内容"
# 关闭 Issue
gh issue close 123 --reason "completed"
# 重新打开
gh issue reopen 123
# 分配 Issue
gh issue edit 123 --add-assignee username# 创建 PR
gh pr create --title "feat: 新功能" --body "描述"
# 列出 PR
gh pr list
# 查看 PR
gh pr view 456
# 检出 PR
gh pr checkout 456
# 合并 PR
gh pr merge 456 --merge
# Squash 合并
gh pr merge 456 --squash
# Rebase 合并
gh pr merge 456 --rebase
# 审查 PR
gh pr review 456 --approve
# 请求更改
gh pr review 456 --request-changes --body "需要修改..."
# 添加标签
gh pr edit 456 --add-label "enhancement"# 获取 Issue 列表(JSON 格式)
gh issue list --json number,title,state
# 使用 jq 处理
gh issue list --json number,title | jq '.[] | "\(.number): \(.title)"'
# 获取 PR 信息
gh pr view 456 --json title,body,author# 调用 API
gh api repos/owner/repo/issues
# 创建 Issue
gh api repos/owner/repo/issues \
--method POST \
-f title="Issue 标题" \
-f body="Issue 内容"
# 更新 Issue
gh api repos/owner/repo/issues/123 \
--method PATCH \
-f state="closed"# 创建别名
gh alias set prc 'pr create --fill'
gh alias set prl 'pr list --state all'
gh alias set iss 'issue list'
# 查看所有别名
gh alias list
# 删除别名
gh alias delete prc- 创建一个新仓库
- 添加 README 文件
- 设置仓库描述
- 添加话题标签
gh repo create practice-project --public
gh repo clone practice-project
echo "# Practice" > README.md
git add . && git commit -m "docs: 添加 README"
git push
gh repo edit practice-project --description "练习项目" --add-topic "git,github,practice"- 创建 3 个 Issue
- 为 Issue 添加标签
- 分配 Issue
- 关闭一个 Issue
gh issue create --title "功能 1" --label "enhancement"
gh issue create --title "功能 2" --label "enhancement"
gh issue create --title "Bug 1" --label "bug"
gh issue close 1 --reason "completed"- 创建一个新分支
- 修改文件并提交
- 创建 PR
- 添加评论
- 合并 PR
git checkout -b feature/new-feature
echo "新功能" > feature.txt
git add . && git commit -m "feat: 添加新功能"
git push -u origin feature/new-feature
gh pr create --title "feat: 添加新功能" --body "实现了新功能"
gh pr comment 1 --body "看起来不错"
gh pr merge 1 --squash# 安装扩展
gh extension install dlvhdr/gh-dash
# 查看已安装扩展
gh extension list
# 使用 dash
gh dash#!/bin/bash
# 批量关闭所有已合并的 PR
gh pr list --state merged --json number | \
jq -r '.[].number' | \
xargs -I {} gh pr close {}
# 批量添加标签
for issue in $(gh issue list --label "needs-triage" --json number | jq -r '.[].number'); do
gh issue edit $issue --add-label "triaged"
done# .github/workflows/auto-label.yml
name: Auto Label
on:
issues:
types: [opened]
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v4
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}- 能够使用
gh repo命令 - 能够使用
gh issue命令 - 能够使用
gh pr命令 - 能够使用 JSON 输出
- 能够使用
gh api调用 API - 能够配置和使用别名
- 能够安装和使用扩展
A: 使用 gh extension upgrade 更新扩展,使用包管理器更新 gh 本身。
A: 设置环境变量:
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080A: 使用 gh api rate_limit 查看。
完成本练习后,你已经掌握了 GitHub CLI 的高级用法。可以继续探索 GitHub Actions 自动化 或 参与开源项目