# Referensi Cepat GitHub CLI

*Repo, issue, PR, action, rilis, API*

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

## Setup

### Instalasi

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

### Autentikasi

```
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
```

### Konfigurasi

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

## Repo

### Perintah 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
```

### Opsi Repo

| Command | Description |
|---------|-------------|
| `--public \| --private` | Atur visibilitas repository |
| `--template owner/repo` | Buat dari template repo |
| `--clone` | Clone setelah membuat |
| `--add-readme` | Inisialisasi dengan README |
| `gh repo list owner` | Tampilkan repo untuk owner |
| `gh repo delete owner/repo` | Hapus repository (dengan konfirmasi) |
| `gh repo rename new-name` | Ganti nama repo saat ini |
| `gh repo archive owner/repo` | Arsipkan repository |

## Issue

### Mengelola Issue

```
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
```

### Opsi Issue

| Command | Description |
|---------|-------------|
| `--assignee @me` | Tetapkan ke diri sendiri |
| `--label bug,urgent` | Tambah label |
| `--milestone v2.0` | Atur milestone |
| `--project "Board"` | Tambah ke project |
| `gh issue edit 42` | Edit issue secara interaktif |
| `gh issue reopen 42` | Buka kembali issue yang ditutup |
| `gh issue comment 42 -b "msg"` | Tambah komentar ke issue |
| `gh issue pin 42` | Pin issue ke repo |

## Pull Request

### Membuat & Mengelola PR

```
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
```

### Opsi PR

| Command | Description |
|---------|-------------|
| `--draft` | Buat sebagai draft PR |
| `--reviewer user1,user2` | Minta reviewer |
| `--base main` | Atur base branch |
| `--merge \| --squash \| --rebase` | Strategi merge |
| `--auto` | Aktifkan auto-merge saat checks lulus |
| `--delete-branch` | Hapus branch setelah merge |
| `gh pr ready 123` | Tandai draft PR sebagai siap |

## Actions

### Perintah 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
```

### Memicu & Mengelola

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

### Opsi Actions

| Command | Description |
|---------|-------------|
| `-f key=value` | Teruskan input ke workflow_dispatch |
| `--json` | Output sebagai JSON |
| `-b branch` | Filter berdasarkan branch |
| `gh run download 12345` | Unduh artifact run |
| `gh cache list` | Tampilkan cache Actions |
| `gh cache delete KEY` | Hapus entri cache |

## Rilis

### Mengelola Rilis

```
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
```

### Opsi Rilis

| Command | Description |
|---------|-------------|
| `--title "Release v1.0"` | Atur judul rilis |
| `--notes "Changelog here"` | Atur catatan rilis secara inline |
| `--notes-file CHANGELOG.md` | Catatan dari file |
| `--generate-notes` | Buat catatan otomatis dari commit |
| `--draft` | Buat sebagai draft |
| `--prerelease` | Tandai sebagai prerelease |
| `--latest` | Tandai sebagai rilis terbaru |
| `gh release download v1.0.0` | Unduh aset rilis |
| `gh release delete v1.0.0` | Hapus rilis |
| `gh release edit v1.0.0` | Edit metadata rilis |

## Gist

### Perintah 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>
```

### Opsi Gist

| Command | Description |
|---------|-------------|
| `-d "description"` | Atur deskripsi gist |
| `--public` | Buat gist publik (default: secret) |
| `--web` | Buka gist di browser |
| `gh gist edit <id>` | Edit file gist |
| `gh gist clone <id>` | Clone gist secara lokal |
| `gh gist delete <id>` | Hapus gist |

## API

### Memanggil 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 } }'
```

### Format 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'
```

### Opsi API

| Command | Description |
|---------|-------------|
| `--method GET\|POST\|PUT\|DELETE` | HTTP method |
| `-f key=value` | Set field string |
| `-F key=@file` | Set field dari file |
| `--jq 'expression'` | Filter JSON dengan sintaks jq |
| `--template 'tmpl'` | Format dengan Go template |
| `--paginate` | Ambil semua halaman |
| `-H 'Accept: ...'` | Set header kustom |

## Alias

### Mengelola Alias

```
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
```

### Alias Lanjutan

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

# Delete alias
gh alias delete co
```

### Tips Alias

| Command | Description |
|---------|-------------|
| `gh alias set name 'cmd'` | Buat alias sederhana |
| `--shell` | Alias berjalan via shell (mendukung pipe) |
| `gh alias list` | Tampilkan semua alias yang didefinisikan |
| `gh alias delete name` | Hapus alias |

## Pola Umum

### Alur Kerja Harian

```
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
```

### Pencarian

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

### Flag Berguna

| Command | Description |
|---------|-------------|
| `--json field1,field2` | Output field tertentu sebagai JSON |
| `--jq 'expr'` | Filter output JSON |
| `-L N` | Batasi hasil ke N item |
| `--web` | Buka hasil di browser |
| `-R owner/repo` | Target repository tertentu |
| `GH_TOKEN=xxx` | Auth via environment variable |
