مهارةEnglish

GitHub CLI gh: pull requests, issues, releases, Actions and the API

Use the gh CLI to open and merge pull requests, triage issues, cut releases, watch and rerun workflow runs, and call the GitHub REST/GraphQL API with jq filters.

تنزيل SKILL.md الملف الخام (Markdown)

التثبيت في Claude Code

ضع الملف في مجلد باسم المهارة داخل مجلد skills الخاص بـ Claude Code، ثم ابدأ جلسة جديدة. الأمر التالي يفعل ذلك:

bash
mkdir -p ~/.claude/skills/github-cli && \
  curl -fsSL https://md2.dev/library/raw/github-cli.md \
  -o ~/.claude/skills/github-cli/SKILL.md

المعاينة

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'

العودة إلى المهارات