gh

GitHub 官方命令列工具,直接在終端操作 GitHub

安裝

# macOS
brew install gh

# Ubuntu/Debian
sudo apt install gh

# Arch Linux
sudo pacman -S github-cli

認證

# 互動式登入
gh auth login

# 檢查認證狀態
gh auth status

# 登出
gh auth logout

倉庫操作

# 克隆倉庫
gh repo clone owner/repo

# 建立新倉庫
gh repo create my-project --public
gh repo create my-project --private

# 在瀏覽器開啟倉庫
gh repo view --web

# 查看倉庫資訊
gh repo view owner/repo

Pull Request

建立 PR

# 互動式建立
gh pr create

# 指定標題和內容
gh pr create --title "Feature: Add login" --body "Description"

# 建立草稿 PR
gh pr create --draft

# 指定 reviewer
gh pr create --reviewer user1,user2

查看和管理 PR

# 列出 PR
gh pr list

# 查看 PR 詳情
gh pr view 123

# 在瀏覽器開啟
gh pr view 123 --web

# 切換到 PR 分支
gh pr checkout 123

# 合併 PR
gh pr merge 123
gh pr merge 123 --squash
gh pr merge 123 --rebase

# 審查 PR
gh pr review 123 --approve
gh pr review 123 --request-changes --body "需要修改..."

Issue

# 列出 issue
gh issue list

# 建立 issue
gh issue create --title "Bug: ..." --body "Description"

# 查看 issue
gh issue view 456

# 關閉 issue
gh issue close 456

Workflow (GitHub Actions)

# 列出 workflow
gh workflow list

# 查看 workflow 執行狀態
gh run list

# 查看特定執行
gh run view 123456

# 重新執行
gh run rerun 123456

# 查看執行日誌
gh run view 123456 --log

Gist

# 建立 gist
gh gist create file.txt

# 建立私密 gist
gh gist create file.txt --public

# 列出 gist
gh gist list

API 操作

# 直接呼叫 GitHub API
gh api repos/owner/repo

# POST 請求
gh api repos/owner/repo/issues --method POST -f title="Bug" -f body="Description"

# 使用 jq 處理輸出
gh api repos/owner/repo/pulls | jq '.[].title'

別名設定

# 設定別名
gh alias set pv 'pr view'

# 使用別名
gh pv 123

# 列出別名
gh alias list