跳到主要内容

GitHub 速查表

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

GitHub CLI 常用命令

认证

gh auth login              # 登录
gh auth status # 查看认证状态
gh auth logout # 登出
gh auth refresh # 刷新认证

仓库

gh repo create             # 创建仓库
gh repo clone owner/repo # 克隆仓库
gh repo fork owner/repo # Fork 仓库
gh repo view # 查看仓库
gh repo delete # 删除仓库
gh repo list # 列出仓库

Issue

gh issue create            # 创建 Issue
gh issue list # 列出 Issue
gh issue view 123 # 查看 Issue
gh issue close 123 # 关闭 Issue
gh issue reopen 123 # 重新打开 Issue
gh issue edit 123 # 编辑 Issue

Pull Request

gh pr create               # 创建 PR
gh pr list # 列出 PR
gh pr view 456 # 查看 PR
gh pr checkout 456 # 检出 PR
gh pr merge 456 # 合并 PR
gh pr close 456 # 关闭 PR
gh pr review 456 # 审查 PR

Actions

gh workflow list           # 列出工作流
gh workflow run name # 运行工作流
gh run list # 列出运行记录
gh run view # 查看运行
gh run watch # 实时查看运行
gh run cancel # 取消运行
gh run rerun # 重新运行

Release

gh release create v1.0.0   # 创建发布
gh release list # 列出发布
gh release view v1.0.0 # 查看发布
gh release delete v1.0.0 # 删除发布
gh release download v1.0.0 # 下载发布

Gist

gh gist create file.txt    # 创建 Gist
gh gist list # 列出 Gist
gh gist view abc123 # 查看 Gist
gh gist edit abc123 # 编辑 Gist
gh gist delete abc123 # 删除 Gist

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

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/

参考链接