跳到主要内容

GitHub 速查表

本文档提供 GitHub 常用命令和配置的快速参考。

GitHub CLI 常用命令

GitHub CLI(gh)是 GitHub 官方的命令行工具,让你可以在终端中高效地操作 GitHub。

认证

gh auth login              # 登录 GitHub
gh auth status # 查看当前认证状态
gh auth logout # 登出当前账户
gh auth refresh # 刷新认证令牌
gh auth switch # 切换活跃账户(多账户场景)
gh auth token # 输出认证令牌

多账户切换示例:

# 查看当前认证状态
gh auth status

# 切换到另一个账户
gh auth switch -u username

# 切换到另一个 GitHub 实例
gh auth switch -h github.example.com

状态查看

gh status                  # 查看你在所有仓库中的工作状态

这个命令会显示:

  • 你被分配的 Issue 和 PR
  • 你发起的 PR 的审查状态
  • 你关注的仓库的最新活动

浏览器打开

gh browse                  # 在浏览器中打开当前仓库
gh browse 123 # 打开 Issue/PR #123
gh browse path/to/file # 打开特定文件
gh browse -s # 打开仓库设置
gh browse -w # 打开 Wiki
gh browse -r # 打开 Releases

Actions 缓存管理

gh cache list              # 列出所有缓存
gh cache list -L 50 # 列出最多 50 个缓存
gh cache delete <cache-id> # 删除指定缓存
gh cache delete --all # 删除所有缓存

仓库

gh repo create                        # 创建新仓库(交互式)
gh repo create my-repo --public # 创建公开仓库
gh repo create my-repo --private # 创建私有仓库
gh repo clone owner/repo # 克隆仓库
gh repo fork owner/repo # Fork 仓库
gh repo fork owner/repo --clone # Fork 并克隆到本地
gh repo view # 查看当前仓库信息
gh repo view owner/repo # 查看指定仓库
gh repo view --web # 在浏览器中打开
gh repo delete owner/repo # 删除仓库
gh repo list # 列出你的仓库
gh repo list owner # 列出指定用户的仓库
gh repo list --limit 50 # 列出最多 50 个
gh repo rename new-name # 重命名当前仓库
gh repo edit --description "描述" # 编辑仓库描述

Issue

gh issue create                       # 创建 Issue(交互式)
gh issue create -t "标题" -b "内容" # 快速创建
gh issue list # 列出当前仓库的 Issue
gh issue list --state closed # 列出已关闭的 Issue
gh issue list -a @me # 列出分配给我的 Issue
gh issue list -l bug # 按标签筛选
gh issue view 123 # 查看 Issue #123
gh issue view 123 --web # 在浏览器中打开
gh issue close 123 # 关闭 Issue
gh issue reopen 123 # 重新打开 Issue
gh issue edit 123 # 编辑 Issue
gh issue develop 123 # 为 Issue 创建关联分支
gh issue transfer 123 owner/other-repo # 将 Issue 转移到其他仓库

Pull Request

gh pr create                          # 创建 PR(交互式)
gh pr create -t "标题" -b "内容" # 快速创建
gh pr create --draft # 创建草稿 PR
gh pr list # 列出 PR
gh pr list --state closed # 列出已关闭的 PR
gh pr list -a @me # 列出我创建的 PR
gh pr list --review-requested @me # 列出等待我审查的 PR
gh pr view 456 # 查看 PR #456
gh pr view 456 --web # 在浏览器中打开
gh pr checkout 456 # 检出 PR 到本地分支
gh pr merge 456 # 合并 PR(交互式选择合并方式)
gh pr merge 456 --merge # 使用 merge commit 合并
gh pr merge 456 --squash # 使用 squash 合并
gh pr merge 456 --rebase # 使用 rebase 合并
gh pr close 456 # 关闭 PR
gh pr reopen 456 # 重新打开 PR
gh pr review 456 # 审查 PR
gh pr review 456 --approve # 批准 PR
gh pr review 456 --request-changes # 请求更改
gh pr review 456 --comment -b "评论" # 添加评论
gh pr ready 456 # 将草稿 PR 标记为就绪
gh pr diff 456 # 查看 PR 的差异

Actions

gh workflow list                      # 列出所有工作流
gh workflow view # 查看工作流详情
gh workflow run workflow-name # 触发工作流
gh workflow run workflow-name -f key=value # 带参数触发
gh run list # 列出运行记录
gh run list --workflow=name # 列出指定工作流的运行
gh run view # 查看最近的运行
gh run view <run-id> # 查看指定运行
gh run watch # 实时查看运行进度
gh run cancel # 取消运行
gh run rerun # 重新运行
gh run rerun --failed # 只重新运行失败的任务
gh run download # 下载运行产物
gh job view <job-id> # 查看任务详情
gh job log <job-id> # 查看任务日志

Release

gh release create v1.0.0              # 创建发布(交互式)
gh release create v1.0.0 --title "标题" --notes "说明"
gh release create v1.0.0 --generate-notes # 自动生成发布说明
gh release create v1.0.0 ./dist/*.zip # 带附件创建发布
gh release list # 列出所有发布
gh release view v1.0.0 # 查看发布详情
gh release view v1.0.0 --web # 在浏览器中打开
gh release download v1.0.0 # 下载发布资源
gh release download v1.0.0 -D ./dir # 下载到指定目录
gh release delete v1.0.0 # 删除发布
gh release edit v1.0.0 --notes "新说明" # 编辑发布

Gist

gh gist create file.txt               # 创建 Gist
gh gist create file.txt --public # 创建公开 Gist
gh gist create -d "描述" file.txt # 带描述创建
gh gist list # 列出你的 Gist
gh gist view abc123 # 查看 Gist
gh gist view abc123 --raw # 查看原始内容
gh gist edit abc123 # 编辑 Gist
gh gist delete abc123 # 删除 Gist
gh gist clone abc123 # 克隆 Gist 到本地

搜索

gh search repos "query"               # 搜索仓库
gh search repos "query" --language python # 按语言筛选
gh search repos "query" --owner facebook # 按所有者筛选
gh search issues "bug" --state open # 搜索 Issue
gh search prs "feature" --author @me # 搜索我的 PR
gh search code "function" # 搜索代码

API 调用

gh api user                          # 获取当前用户信息
gh api repos/owner/repo # 获取仓库信息
gh api -X POST repos/owner/repo/issues -f title="标题" # 创建 Issue
gh api --paginate user/repos # 分页获取所有仓库
gh api graphql -f query='{...}' # 执行 GraphQL 查询

别名配置

gh alias set pc 'pr create'           # 设置别名
gh alias set co 'pr checkout'
gh alias set ic 'issue create'
gh alias set ls 'issue list -a @me'
gh alias list # 列出所有别名
gh alias delete pc # 删除别名

扩展管理

gh extension list                     # 列出已安装的扩展
gh extension install owner/extension # 安装扩展
gh extension upgrade --all # 升级所有扩展
gh extension remove name # 移除扩展
gh extension search # 搜索可用扩展

配置

gh config get editor                  # 获取配置值
gh config set editor "code -w" # 设置默认编辑器
gh config set git_protocol ssh # 设置 Git 协议
gh config list # 列出所有配置
gh config clear-cache # 清除缓存

Git 常用命令

初始化和克隆

git init                   # 初始化仓库
git clone url # 克隆仓库
git clone url --depth 1 # 浅克隆

基本操作

git status                 # 查看状态
git add . # 添加所有文件
git add file.txt # 添加指定文件
git commit -m "message" # 提交
git commit --amend # 修改上次提交

分支操作

git branch                 # 列出分支
git branch feature # 创建分支
git checkout feature # 切换分支
git checkout -b feature # 创建并切换分支
git branch -d feature # 删除分支
git merge feature # 合并分支

远程操作

git remote -v              # 查看远程仓库
git remote add origin url # 添加远程仓库
git push origin main # 推送
git push -u origin main # 推送并设置上游
git pull origin main # 拉取
git fetch origin # 获取远程更新

撤销操作

git checkout -- file.txt   # 撤销工作区修改
git reset HEAD file.txt # 撤销暂存
git reset --soft HEAD~1 # 软回退(保留修改)
git reset --hard HEAD~1 # 硬回退(丢弃修改)
git revert commit # 创建撤销提交

查看历史

git log                    # 查看提交历史
git log --oneline # 简洁历史
git log --graph # 图形化历史
git diff # 查看差异
git diff branch1 branch2 # 比较分支
git show commit # 查看提交详情

标签

git tag                    # 列出标签
git tag v1.0.0 # 创建轻量标签
git tag -a v1.0.0 -m "msg" # 创建附注标签
git push origin v1.0.0 # 推送标签
git push origin --tags # 推送所有标签
git tag -d v1.0.0 # 删除本地标签

Stash

git stash                  # 暂存修改
git stash list # 列出暂存
git stash pop # 恢复并删除
git stash apply # 恢复但不删除
git stash drop # 删除暂存

GitHub Actions 工作流模板

基础 CI

name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Hello World"

Node.js 项目

name: Node.js CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test

Python 项目

name: Python CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: pip install -r requirements.txt
- run: pytest

Docker 构建

name: Docker
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:latest

Codespaces CLI 命令

gh codespace list              # 列出所有 codespaces
gh codespace create # 创建 codespace
gh codespace create -r owner/repo # 指定仓库创建
gh codespace connect # 连接到 codespace
gh codespace stop # 停止 codespace
gh codespace delete # 删除 codespace
gh codespace ports # 查看端口转发
gh codespace rebuild # 重建容器

Dev Container 配置模板

基础 Node.js 项目

{
"name": "Node.js Project",
"image": "mcr.microsoft.com/devcontainers/javascript-node:20",
"features": {
"ghcr.io/devcontainers/features/node:1": { "version": "20" }
},
"postCreateCommand": "npm install",
"customizations": {
"vscode": {
"extensions": ["esbenp.prettier-vscode"]
}
}
}

Python 项目

{
"name": "Python Project",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"features": {
"ghcr.io/devcontainers/features/python:1": { "version": "3.12" }
},
"postCreateCommand": "pip install -r requirements.txt"
}

使用 Dockerfile

{
"name": "Custom Environment",
"build": {
"dockerfile": "Dockerfile"
},
"postCreateCommand": "make setup"
}

Projects 筛选语法

# 状态筛选
status:"In Progress"
status:Done

# 指派人
assignee:@me
assignee:username

# 标签
label:bug
label:enhancement

# 迭代
iteration:@current
iteration:"Sprint 1"

# 日期
target:<=2024-03-15

# 组合
status:"In Progress" label:bug
-status:Done

Projects GraphQL API 示例

# 查询项目项目
query {
node(id: "PROJECT_ID") {
... on ProjectV2 {
items(first: 10) {
nodes {
id
content {
... on Issue {
title
number
}
}
}
}
}
}
}

Dependabot 配置

version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10

常用工作流触发器

on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
release:
types: [published]

常用上下文变量

变量说明
github.event_name触发事件
github.ref分支或标签引用
github.sha提交 SHA
github.repository仓库名
github.workflow工作流名
github.run_id运行 ID
github.actor触发者
secrets.NAMESecret 值
env.NAME环境变量

常用条件表达式

if: github.ref == 'refs/heads/main'
if: startsWith(github.ref, 'refs/tags/')
if: github.event_name == 'push'
if: success()
if: failure()
if: always()
if: cancelled()

SSH 配置

ssh-keygen -t ed25519 -C "[email protected]"
ssh-add ~/.ssh/id_ed25519
ssh -T [email protected]

Git 配置

git config --global user.name "Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"

.gitignore 模板

.DS_Store
.idea/
*.log
node_modules/
dist/
.env
*.local
coverage/

参考链接