Getting started
Introduction
GitHub CLI works with pull requests, issues, and
Actions from the terminal. This reference covers the most-used commands,
JSON output, and gh api.
- GitHub CLI manual (cli.github.com)
- Documentation (cli.github.com)
- GitHub repository (github.com)
- Installation (github.com)
Install
brew install gh
winget install --id GitHub.cli
sudo apt install gh
sudo dnf install gh
See: Installation
Sign in
gh auth login
gh auth status
gh auth switch
gh auth setup-git
gh auth login walks through a browser or token flow. setup-git wires git to use gh as its credential helper.
See: gh auth login
Check your queue
gh status
gh pr status
gh issue status
gh status combines assigned issues and PRs, review requests, mentions, and recent activity.
See: gh status
Repositories
Clone
Create
Interactive
# prompts for name and visibility
gh repo create
From this directory
gh repo create my-tool --private \
--source=. --push
--source uses the current directory's git history; --push uploads existing commits.
See: gh repo create
Fork
Clone
gh repo fork cli/cli --clone
Organization
gh repo fork --org my-org \
--remote
The fork becomes origin; the original repository is added as upstream.
See: gh repo fork
View and open
Repo details
gh repo view
gh repo view cli/cli --web
Files, issues, and PRs
gh browse src/main.ts
gh browse 42
gh browse -n 42
gh browse accepts an issue or PR number, file path, or commit SHA. -n prints the URL instead of opening it.
See: gh browse
Pull requests
Create
Interactive
gh pr create
Non-interactive
gh pr create --fill --base develop
gh pr create -t "Fix login" -F -
--fill uses the commits for the title and body; -F - reads the body from stdin. An unpushed branch still prompts.
See: gh pr create
List and filter
All
gh pr list
By author
gh pr list --author @me \
--state all
By search
gh pr list --search \
"review:required"
By label
gh pr list --label bug -L 50
--search takes GitHub search syntax; --label repeats for multiple labels.
See: gh pr list
Inspect and check out
View
gh pr view 123
gh pr view 123 --comments
Check out
gh pr checkout 123
Diff
gh pr diff 123 --name-only
gh pr checkout creates a local branch that tracks the PR head.
See: gh pr checkout
Watch CI checks
Status
gh pr checks
Watch
gh pr checks --watch \
--fail-fast
Required
gh pr checks --required
--watch refreshes until checks finish; --required shows only required checks.
See: gh pr checks
Review and comment
Approve
gh pr review --approve
Request changes
gh pr review \
--request-changes -b "Add tests"
Comment
gh pr comment -b "LGTM"
gh pr comment --edit-last -F -
gh pr review approves or requests changes; gh pr comment leaves a plain comment, and --edit-last updates your last one.
See: gh pr review
Merge
Squash
gh pr merge --squash \
--delete-branch
Auto-merge
gh pr merge --auto --rebase
Merge commit
gh pr merge 123 --merge
--auto enables auto-merge; if the base branch uses a merge queue, the PR joins it once required checks pass.
See: gh pr merge
Issues
Create
With title and body
gh issue create \
-t "Login fails" \
-b "Steps to reproduce"
With label and assignee
gh issue create -l bug --assignee @me
As a sub-issue
gh issue create --parent 100
See: gh issue create
List and filter
All
gh issue list
By assignee
gh issue list -a @me -l bug
By search
gh issue list --search \
"is:open no:assignee"
By state
gh issue list --state all -L 50
-a @me filters to your issues; --search takes GitHub search syntax such as no:assignee.
See: gh issue list
Read and comment
Read
gh issue view 123 --comments
Comment
gh issue comment 123 \
-b "Fixed in #456"
Open in browser
gh issue view 123 --web
--comments (-c) includes the comment thread; --web opens the issue.
See: gh issue view
Close
Default
gh issue close 123
With a reason
gh issue close 123 \
--reason "not planned"
As duplicate
gh issue close 123 \
--duplicate-of 456
--duplicate-of closes the issue as a duplicate of the one you pass; a Fixes #123 line in a PR closes it on merge.
See: gh issue close
Actions
List runs
Recent
gh run list
Failures by workflow
gh run list \
--workflow ci.yml \
--status failure
By branch
gh run list --branch main \
--limit 10
--status also takes in_progress, queued, and success.
See: gh run list
Watch a run
Watch
gh run watch 123456789
Fail on error
gh run watch --compact \
--exit-status
--exit-status fails the command when the run fails, which suits CI scripts.
See: gh run watch
Inspect and rerun
Failed logs
gh run view 123456789 \
--log-failed
Rerun failed
gh run rerun 123456789 --failed
--log-failed prints only failed steps; --failed reruns only failed jobs.
See: gh run view, gh run rerun
Run a workflow
List
gh workflow list
Run
gh workflow run ci.yml
With inputs
gh workflow run deploy.yml \
-f environment=staging
On a branch
gh workflow run deploy.yml \
--ref my-branch
-f sets a workflow_dispatch input; repeat it for more inputs.
See: gh workflow run
Releases
Create
Generated notes
gh release create v1.2.0 \
--generate-notes
Draft
gh release create v1.2.0 \
--draft -F notes.md
Assets
gh release create v1.2.0 \
dist/*.tgz
--generate-notes writes notes from merged PRs; assets after the tag are uploaded with the release.
See: gh release create
Download
By tag
gh release download v1.2.0
By pattern
gh release download \
--pattern '*.deb'
gh release download \
-p '*.rpm' -D /tmp
Source archive
gh release download v1.2.0 \
--archive=zip
Without a tag, pass --pattern or --archive; gh uses the latest release. --archive fetches source, not assets.
See: gh release download
List and view
List
gh release list
View
gh release view v1.2.0
Open in browser
gh release view --web
See: gh release view
Search and API
Search repositories
By language
gh search repos \
"static site generator" \
--language go
By owner and topic
gh search repos --owner cli \
--topic gh-extension
By stars
gh search repos "database" \
--sort stars --limit 10
--sort takes stars, forks, updated, or help-wanted-issues.
See: gh search repos
Search issues and PRs
Issues
gh search issues "panic" \
--repo cli/cli
Review requests
gh search prs \
--review-requested=@me \
--state=open
Your drafts
gh search prs --draft \
--author=@me
Both accept a free-text query and field filters such as --author and --state.
Call the API
Read
gh api repos/cli/cli
Filter
gh api repos/cli/cli \
--jq '.stargazers_count'
Create
gh api repos/{owner}/{repo}/issues \
-f title='Bug'
--jq filters the response; {owner} and {repo} expand from the current repo. -f sends a string and switches to POST, so the issues line creates an issue.
See: gh api
GraphQL
gh api graphql \
-f query='{ viewer { login } }'
Queries the GraphQL API; -f query= sends the document.
See: GitHub GraphQL API
JSON output
Fields
gh pr list --json \
number,title,author
jq
gh pr list --json number,title \
--jq '.[].title'
Template
gh pr list --json number \
--template \
'{{range .}}#{{.number}} {{end}}'
--jq and --template need --json; omit the field list to see available fields.
See: gh help formatting
Configuration
Target another repo
gh pr list -R cli/cli
gh issue view 42 -R cli/cli
gh release list -R cli/cli
-R takes OWNER/REPO for one command.
See: gh manual
Aliases
Plain
gh alias set pv 'pr view'
gh alias set bugs \
'issue list --label bug'
Shell
gh alias set --shell count \
'gh pr list --json number | jq length'
List
gh alias list
Shell aliases run through sh, so pipes and other commands work.
See: gh alias set
Extensions
gh extension install dlvhdr/gh-dash
gh extension list
gh extension upgrade --all
Extensions add commands that run as gh <name>, such as gh dash.
See: gh extension
Config
gh config set editor nvim
gh config set git_protocol ssh
gh config list
On Linux, the config directory is $GH_CONFIG_DIR, else $XDG_CONFIG_HOME/gh, else ~/.config/gh.
See: gh config
Environment
export GH_TOKEN=$(gh auth token)
export GH_REPO=cli/cli
export GH_HOST=github.com
export GH_CONFIG_DIR=~/.config/gh
GH_TOKEN takes precedence over stored credentials; GH_REPO and GH_HOST set defaults, and GH_CONFIG_DIR overrides the config directory.
See: gh help environment
0 Comments for this cheatsheet. Write yours!