---
name: github-cli
description: 'Drive GitHub from the terminal with gh: create/review/merge pull requests, list and file issues, create releases with assets, watch or rerun GitHub Actions runs and read failed logs, call the REST and GraphQL API with gh api and --jq. Use when the user asks to open a PR, check CI, merge, release, or query GitHub data. Triggers: gh pr create، github actions logs، gh release، gh api، open a pull request.'
---

# GitHub CLI (gh)

## When to use

Any GitHub task that has a command: pull requests, issues, releases, Actions, repo settings, API queries. Prefer `gh` to scraping web pages.

## Prerequisites

```bash
gh --version
gh auth status                       # logged in? else: gh auth login
gh auth refresh -s workflow          # add a missing scope
# CI: export GH_TOKEN=<token>        # no interactive login needed
```

Run inside a clone: `gh` infers the repository from the git remote, or pass `-R owner/repo`.

## Pull request workflow

1. Push the branch: `git push -u origin HEAD`.
2. Open the PR with a real title and body.
3. Watch checks, address review comments, then merge.

```bash
gh pr create --base main --title "feat(export): add EPUB" --body-file pr.md
gh pr create --fill --draft                       # title and body from commits
gh pr list --author @me --state open
gh pr view 42 --comments
gh pr diff 42 --name-only
gh pr checkout 42
gh pr checks 42 --watch                           # block until CI finishes
gh pr review 42 --approve --body "LGTM"
gh pr review 42 --request-changes --body "see inline"
gh pr merge 42 --squash --delete-branch           # add --auto to wait for checks
gh pr ready 42                                    # draft to ready
```

## Issues

```bash
gh issue create --title "Tables overflow in RTL" --body "Steps: ..." --label bug
gh issue list --label bug --assignee @me --json number,title --jq '.[] | "\(.number) \(.title)"'
gh issue comment 17 --body "Fixed in #42"
gh issue close 17 --reason completed
```

## Releases

```bash
git tag -a v1.4.0 -m "v1.4.0" && git push origin v1.4.0
gh release create v1.4.0 --generate-notes --latest ./dist/app.zip ./dist/app.tar.gz
gh release upload v1.4.0 ./dist/extra.zip --clobber
gh release list --limit 5
gh release download v1.4.0 --pattern "*.zip"
```

## Actions

```bash
gh run list --workflow ci.yml --limit 5
gh run view <run-id> --log-failed                 # only the failing steps
gh run watch <run-id> --exit-status               # non-zero exit if the run fails
gh run rerun <run-id> --failed
gh workflow run deploy.yml -f environment=staging
gh workflow list
```

## API

`{owner}` and `{repo}` are filled from the current repository.

```bash
gh api repos/{owner}/{repo}/pulls/42/comments --paginate --jq '.[] | {user: .user.login, body}'
gh api -X POST repos/{owner}/{repo}/issues -f title="Bug" -f body="Details"
gh api repos/{owner}/{repo}/branches/main/protection
gh api graphql -f query='query { viewer { login } }'
gh api --cache 1h repos/{owner}/{repo}/releases/latest --jq .tag_name
```

## Repository

```bash
gh repo clone owner/repo
gh repo create my-app --private --source . --push
gh repo view --web
gh search issues "is:open label:bug repo:owner/repo" --json number,title
```

## Gotchas

- `gh pr merge` with no flag asks questions in a terminal; always pass `--squash`, `--merge` or `--rebase` in scripts.
- Use `--body-file` for long bodies so shell quoting cannot mangle them.
- `--json` needs explicit field names (run it with an invalid field to list them); combine with `--jq` instead of piping to `jq`.
- `gh run watch` polls; add `--interval 10` to be gentle on rate limits.
- Pushing workflow files needs the `workflow` token scope.
- Never print tokens: `gh auth token` is for piping only.

## Verify

```bash
gh pr view --json state,mergeable,statusCheckRollup --jq '{state, mergeable}'
gh pr checks                  # all green before merging
gh release view v1.4.0 --json assets --jq '.assets[].name'
```
