# Tham Khảo Nhanh GitHub CLI

*Repos, issues, PRs, actions, releases, API*

> Source: GitHub CLI Documentation (cli.github.com) · MIT

## Cài Đặt

### Cài Đặt

| Command | Description |
|---------|-------------|
| `brew install gh` | macOS qua Homebrew |
| `sudo apt install gh` | Debian / Ubuntu |
| `winget install GitHub.cli` | Windows qua winget |
| `conda install gh` | Qua conda-forge |

### Xác Thực

```
gh auth login                  # interactive login
gh auth login --with-token < token.txt
gh auth status                 # check auth state
gh auth refresh -s repo,gist   # add scopes
```

### Cấu Hình

```
gh config set editor vim
gh config set pager less
gh config set git_protocol ssh
gh config list
```

## Repos

### Lệnh Repository

```
gh repo create my-app --public --clone
gh repo clone owner/repo
gh repo fork owner/repo --clone
gh repo view owner/repo --web
```

### Tùy Chọn Repo

| Command | Description |
|---------|-------------|
| `--public \| --private` | Thiết lập khả năng hiển thị repository |
| `--template owner/repo` | Tạo từ template repo |
| `--clone` | Clone sau khi tạo |
| `--add-readme` | Khởi tạo với README |
| `gh repo list owner` | Liệt kê repos cho owner |
| `gh repo delete owner/repo` | Xóa repository (có xác nhận) |
| `gh repo rename new-name` | Đổi tên repo hiện tại |
| `gh repo archive owner/repo` | Lưu trữ repository |

## Issues

### Quản Lý Issues

```
gh issue create --title "Bug" --body "Details here"
gh issue list --state open --label bug
gh issue view 42
gh issue close 42 --reason completed
```

### Tùy Chọn Issue

| Command | Description |
|---------|-------------|
| `--assignee @me` | Giao cho bản thân |
| `--label bug,urgent` | Thêm nhãn |
| `--milestone v2.0` | Thiết lập milestone |
| `--project "Board"` | Thêm vào project |
| `gh issue edit 42` | Chỉnh sửa issue tương tác |
| `gh issue reopen 42` | Mở lại issue đã đóng |
| `gh issue comment 42 -b "msg"` | Thêm bình luận vào issue |
| `gh issue pin 42` | Ghim issue vào repo |

## Pull Requests

### Tạo & Quản Lý PRs

```
gh pr create --title "feat: add auth" --body "..."
gh pr create --fill          # title/body from commits
gh pr list --state open
gh pr view 123 --web
```

### Review & Merge

```
gh pr checkout 123           # check out PR branch
gh pr diff 123               # view PR diff
gh pr review 123 --approve
gh pr merge 123 --squash --delete-branch
```

### Tùy Chọn PR

| Command | Description |
|---------|-------------|
| `--draft` | Tạo PR dạng draft |
| `--reviewer user1,user2` | Yêu cầu reviewer |
| `--base main` | Thiết lập base branch |
| `--merge \| --squash \| --rebase` | Chiến lược merge |
| `--auto` | Bật auto-merge khi checks pass |
| `--delete-branch` | Xóa branch sau khi merge |
| `gh pr ready 123` | Đánh dấu draft PR là sẵn sàng |

## Actions

### Lệnh Workflow

```
gh run list                         # recent runs
gh run view 12345                   # run details
gh run view 12345 --log-failed      # failed step logs
gh run watch 12345                  # live status
```

### Kích Hoạt & Quản Lý

```
gh workflow run deploy.yml --ref main
gh workflow list
gh workflow view deploy.yml
gh run rerun 12345 --failed   # rerun failed jobs
```

### Tùy Chọn Actions

| Command | Description |
|---------|-------------|
| `-f key=value` | Truyền input cho workflow_dispatch |
| `--json` | Output dạng JSON |
| `-b branch` | Lọc theo branch |
| `gh run download 12345` | Tải xuống artifacts của run |
| `gh cache list` | Liệt kê Actions caches |
| `gh cache delete KEY` | Xóa một cache entry |

## Releases

### Quản Lý Releases

```
gh release create v1.0.0 --generate-notes
gh release create v1.0.0 ./dist/*.tar.gz
gh release list
gh release view v1.0.0
```

### Tùy Chọn Release

| Command | Description |
|---------|-------------|
| `--title "Release v1.0"` | Thiết lập tiêu đề release |
| `--notes "Changelog here"` | Thiết lập ghi chú release trực tiếp |
| `--notes-file CHANGELOG.md` | Ghi chú từ file |
| `--generate-notes` | Tự động tạo từ commits |
| `--draft` | Tạo dạng draft |
| `--prerelease` | Đánh dấu là prerelease |
| `--latest` | Đánh dấu là release mới nhất |
| `gh release download v1.0.0` | Tải xuống release assets |
| `gh release delete v1.0.0` | Xóa một release |
| `gh release edit v1.0.0` | Chỉnh sửa metadata release |

## Gists

### Lệnh Gist

```
gh gist create file.py -d "My snippet"
gh gist create file1.js file2.js   # multi-file gist
gh gist list
gh gist view <id>
```

### Tùy Chọn Gist

| Command | Description |
|---------|-------------|
| `-d "description"` | Thiết lập mô tả gist |
| `--public` | Tạo gist công khai (mặc định: bí mật) |
| `--web` | Mở gist trong trình duyệt |
| `gh gist edit <id>` | Chỉnh sửa file gist |
| `gh gist clone <id>` | Clone gist về máy |
| `gh gist delete <id>` | Xóa gist |

## API

### Gọi API

```
gh api repos/owner/repo
gh api repos/owner/repo/issues --method POST \
  -f title="Bug" -f body="Details"
gh api graphql -f query='{ viewer { login } }'
```

### Định Dạng Output

```
gh api repos/owner/repo --jq '.stargazers_count'
gh api repos/owner/repo --template '{{.full_name}}'
gh pr list --json number,title --jq '.[].title'
```

### Tùy Chọn API

| Command | Description |
|---------|-------------|
| `--method GET\|POST\|PUT\|DELETE` | Phương thức HTTP |
| `-f key=value` | Thiết lập trường chuỗi |
| `-F key=@file` | Thiết lập trường từ file |
| `--jq 'expression'` | Lọc JSON bằng cú pháp jq |
| `--template 'tmpl'` | Định dạng bằng Go template |
| `--paginate` | Lấy tất cả trang |
| `-H 'Accept: ...'` | Thiết lập header tùy chỉnh |

## Aliases

### Quản Lý Aliases

```
gh alias set co 'pr checkout'
gh alias set bugs 'issue list --label bug'
gh alias set last 'run list -L 1'
gh alias list
```

### Aliases Nâng Cao

```
# Alias with shell command
gh alias set --shell pv 'gh pr view --json url --jq .url | pbcopy'

# Delete alias
gh alias delete co
```

### Mẹo Alias

| Command | Description |
|---------|-------------|
| `gh alias set name 'cmd'` | Tạo alias đơn giản |
| `--shell` | Alias chạy qua shell (hỗ trợ pipe) |
| `gh alias list` | Hiển thị tất cả aliases đã định nghĩa |
| `gh alias delete name` | Xóa một alias |

## Mẫu Phổ Biến

### Quy Trình Hàng Ngày

```
gh issue list --assignee @me        # my open issues
gh pr status                        # PRs needing attention
gh pr checks 123                    # CI status for PR
gh run list -b main -L 5            # recent CI runs
```

### Tìm Kiếm

```
gh search repos --language rust --stars ">1000"
gh search issues --repo owner/repo "memory leak"
gh search prs --state open --review required
```

### Cờ Hữu Ích

| Command | Description |
|---------|-------------|
| `--json field1,field2` | Output các trường cụ thể dạng JSON |
| `--jq 'expr'` | Lọc JSON output |
| `-L N` | Giới hạn kết quả N mục |
| `--web` | Mở kết quả trong trình duyệt |
| `-R owner/repo` | Nhắm mục tiêu repository cụ thể |
| `GH_TOKEN=xxx` | Xác thực qua biến môi trường |
