07 - GitHub CLI 与高级技巧
本章目标:掌握 GitHub CLI(gh)命令行工具,学会 GitHub 高级操作技巧。
一、安装 GitHub CLI
1.1 安装
bash
# Windows
winget install GitHub.cli
# 或者下载安装包
# https://cli.github.com/
# 验证安装
gh --version
1.2 登录
bash
# 登录 GitHub
gh auth login
# 选择:
# 1. GitHub.com
# 2. HTTPS
# 3. Login with a web browser(推荐)
# 浏览器会打开,输入验证码完成登录
二、gh 常用命令
2.1 仓库操作
bash
# 创建新仓库
gh repo create my-new-repo --public
gh repo create my-new-repo --private
# 克隆仓库
gh repo clone owner/repo
# 查看仓库信息
gh repo view
# 在浏览器中打开仓库
gh repo view --web
# 列出你的仓库
gh repo list
2.2 Issue 操作
bash
# 创建 Issue
gh issue create --title "Bug: login failed" --body "描述问题"
# 列出 Issue
gh issue list
gh issue list --label "bug"
gh issue list --assignee "@me"
# 查看 Issue
gh issue view 123
# 关闭 Issue
gh issue close 123
# 重新打开 Issue
gh issue reopen 123
2.3 Pull Request 操作
bash
# 创建 PR
gh pr create --title "feat: add login" --body "实现登录功能"
# 列出 PR
gh pr list
gh pr list --state merged
# 查看 PR
gh pr view 456
# 检出 PR 到本地
gh pr checkout 456
# 合并 PR
gh pr merge 456
# 查看 PR 检查状态
gh pr checks 456
2.4 GitHub Actions 操作
bash
# 查看工作流列表
gh workflow list
# 查看工作流运行记录
gh run list
# 查看具体运行的详情
gh run view 12345
# 下载运行日志
gh run view 12345 --log
# 手动触发工作流
gh workflow run deploy.yml
# 监视运行中的工作流
gh run watch
2.5 Release 操作
bash
# 列出 Release
gh release list
# 创建 Release
gh release create v1.0.0 --title "v1.0.0" --notes "首次发布"
# 上传附件
gh release create v1.0.0 --title "v1.0.0" ./dist/app.zip
# 下载 Release 附件
gh release download v1.0.0
三、gh 扩展
3.1 安装扩展
bash
# 搜索扩展
gh extension search notify
# 安装扩展
gh extension install dlvhdr/gh-dash # PR/Issue 仪表盘
gh extension install surajin/gh-notify # 通知管理
gh extension install github/gh-copilot # AI 辅助
# 列出已安装扩展
gh extension list
# 更新扩展
gh extension upgrade
3.2 推荐扩展
bash
# gh-dash:漂亮的 PR/Issue 仪表盘
gh extension install dlvhdr/gh-dash
gh dash
# gh-copilot:AI 代码助手
gh extension install github/gh-copilot
gh copilot suggest "create a CI workflow"
四、GitHub 高级搜索
4.1 搜索语法
# 搜索仓库
language:javascript stars:>1000 topic:react
# 搜索 Issue
is:issue is:open label:bug repo:owner/repo
# 搜索 PR
is:pr is:merged author:username
# 搜索代码
filename:package.json "react" org:facebook
# 搜索用户
type:user location:china language:python
4.2 常用搜索
# 找好的第一个 Issue
label:"good first issue" language:javascript stars:>100 is:open
# 找你参与的 PR
is:pr author:your-name is:open
# 找最近的 Release
is:release created:>2024-01-01
五、GitHub API(进阶)
5.1 用 gh 调用 API
bash
# 获取仓库信息
gh api repos/owner/repo
# 获取 Issue 列表
gh api repos/owner/repo/issues
# 创建 Issue
gh api repos/owner/repo/issues \
--method POST \
--field title="Bug report" \
--field body="描述问题..."
# 获取当前用户信息
gh api user
5.2 用 curl 调用 API
bash
# 获取仓库信息
curl -H "Authorization: token YOUR_TOKEN" \
https://api.github.com/repos/owner/repo
# 获取仓库列表
curl -H "Authorization: token YOUR_TOKEN" \
https://api.github.com/user/repos
六、GitHub 桌面客户端
如果你不习惯命令行,GitHub Desktop 是个不错的选择。
6.1 下载安装
https://desktop.github.com/
6.2 常用操作
- Clone 仓库
- Create branch
- Commit changes
- Pull / Push
- Create PR
- View history
- Resolve merge conflicts
七、GitHub 常用快捷键
7.1 全局快捷键
T → 跳转文件
. → 用 GitHub.dev 打开编辑器
/ → 搜索框
7.2 文件页面
L → 跳转到某一行
B → 打开 Blame
R → 查看原始文件
7.3 Issue/PR 页面
X → 选中/取消
C → 提交评论
E → 编辑
八、GitHub.dev 在线编辑器
在任意仓库页面按 "." 键
会打开 VS Code 风格的在线编辑器
功能:
- 编辑代码
- 创建分支
- 提交更改
- 创建 PR
- 终端(有限)
九、GitHub Copilot(AI 编程助手)
9.1 简介
GitHub Copilot 是 AI 编程助手:
- 自动补全代码
- 根据注释生成代码
- 解释代码
- 修复 Bug
9.2 使用
1. 安装 VS Code 扩展
2. 登录 GitHub 账号
3. 开始编码,Copilot 会自动提供建议
十、实战练习
在你的
my-git-practice仓库中完成以下操作:
bash
# 1. 安装并登录 gh
gh auth login
# 2. 用 gh 创建一个 Issue
gh issue create --title "练习 Issue" --body "这是用 gh 创建的 Issue"
# 3. 用 gh 查看 Issue
gh issue list
# 4. 用 gh 创建一个 PR
gh pr create --title "feat: add practice" --body "练习用 gh 创建 PR"
# 5. 用 gh 合并 PR
gh pr merge --delete-branch
# 6. 用 gh 查看 Actions 运行状态
gh run list
十一、练习清单
- 安装 GitHub CLI(gh)
- 用 gh 登录 GitHub
- 用 gh 创建仓库
- 用 gh 创建和管理 Issue
- 用 gh 创建和管理 PR
- 用 gh 查看 Actions 运行状态
- 尝试安装一个 gh 扩展
- 用 GitHub 搜索语法找感兴趣的项目
- 用 GitHub.dev 编辑器修改一个文件
十二、学习资源
GitHub 官方文档:https://docs.github.com
GitHub Skills:https://skills.github.com
GitHub Learning Lab:https://lab.github.com
GitHub Blog:https://github.blog
GitHub YouTube:https://youtube.com/github
上一章 :06-GitHub Pages部署网站(06-GitHub Pages部署网站.md)
返回目录 :<README.md>