Skip to main content

GitHub CLI Cheatsheet

By Dejan Panovski Updated on Download PDF

gh at a glance: authentication, repositories, pull requests, issues, GitHub Actions runs and secrets, releases, search, extensions, and the API.

The gh command brings GitHub itself into the terminal, covering the parts of a project that live on the server rather than in the repository: pull requests, issues, workflow runs, releases, and secrets. This cheatsheet collects the subcommands and flags you reach for daily, plus the JSON output options that make gh usable inside scripts.

Authentication

Log in once per host, then check what the stored token can do.

Command or variableDescription
gh auth loginStart the interactive login flow
gh auth login --webAuthenticate through the browser
gh auth login --hostname github.example.comLog in to a GitHub Enterprise Server host
gh auth statusShow the active account, protocol, and token scopes
gh auth refresh -s workflowAdd a scope to the existing token
gh auth switchChange the active account
gh auth tokenPrint the raw token gh is using
gh auth setup-gitConfigure git to authenticate through gh
gh auth logoutRemove stored credentials for a host
GH_TOKENToken environment variable for GitHub.com automation
GH_ENTERPRISE_TOKENToken environment variable for GitHub Enterprise Server automation

Treat the output of gh auth token as a password. Store automation tokens in your CI secret manager, expose them through the appropriate environment variable, and never commit them to a repository.

Repositories

Create, clone, and inspect repositories without leaving the shell.

CommandDescription
gh repo clone OWNER/REPOClone a repository using your configured protocol
gh repo viewShow the description, default branch, and README
gh repo view --webOpen the repository in a browser
gh repo create NAME --private --source=. --pushPublish the current directory as a new repository
gh repo fork --cloneFork a repository and clone the fork
gh repo syncUpdate a fork from its upstream
gh repo list OWNERList repositories owned by a user or organization
gh repo edit --default-branch mainSet main as the default branch
gh repo set-default OWNER/REPOPin the repository that commands target here
gh repo archiveArchive a repository

Pull Requests

The commands that replace most of the pull request web interface.

CommandDescription
gh pr listList open pull requests
gh pr list --state all --author @meList your pull requests in every state
gh pr statusShow pull requests relevant to you
gh pr create --fillOpen a pull request using the commit title and body
gh pr create --draft --reviewer octocatCreate a draft and request a review
gh pr checkout NUMBERCheck the pull request out as a local branch
gh pr diff NUMBERShow the patch
gh pr checks NUMBERShow the status checks and their conclusions
gh pr review --approveApprove the pull request for the current branch
gh pr merge NUMBER --squashSquash the commits and merge the pull request
gh pr ready NUMBERMark a draft as ready for review
gh pr view NUMBER --webOpen the pull request in a browser

Issues

Triage and close issues from the same terminal you build in.

CommandDescription
gh issue listList open issues
gh issue list --label bug --assignee @meList open bugs assigned to you
gh issue statusShow issues assigned to or mentioning you
gh issue create --title "..." --body "..."Create an issue without the prompts
gh issue view NUMBERRead an issue in the terminal
gh issue comment NUMBER --body "..."Add a comment
gh issue edit NUMBER --add-label bugAdd the bug label to an issue
gh issue close NUMBER --comment "..."Close with a closing comment
gh issue develop NUMBER --checkoutCreate and check out a branch linked to the issue
gh issue transfer NUMBER OWNER/REPOMove an issue to another repository

GitHub Actions

Watch, debug, and re-run workflows from the command line.

CommandDescription
gh run listList recent workflow runs
gh run list --workflow ci.yml --status failureList failed runs for ci.yml
gh run view IDShow a summary of one run
gh run view ID --log-failedPrint the logs for failed steps only
gh run watch IDFollow a run until it finishes
gh run rerun ID --failedRe-run only the jobs that failed
gh run download IDDownload the artifacts a run produced
gh run cancel IDCancel a run in progress
gh workflow listList the workflows in the repository
gh workflow run build.yml -f env=stagingTrigger a workflow_dispatch workflow with inputs
gh workflow disable NAMEDisable a workflow
gh cache listList the Actions caches for the repository
gh cache delete --allImmediately delete every Actions cache

Deleting all Actions caches has no dry-run and can slow the next workflow runs while the caches rebuild. Confirm that the command targets the intended repository before running it.

Secrets and Variables

Secrets are write-only; variables stay readable.

CommandDescription
gh secret set NAMEPrompt for a value and store it, hidden from shell history
echo "$VALUE" | gh secret set NAMERead the value from standard input
gh secret set --env-file .envLoad several secrets from a dotenv file
gh secret set NAME --env productionSet a deployment environment secret
gh secret set NAME --org ORG --visibility selected --repos a,bSet an organization secret for named repositories
gh secret listList secret names and update times, never values
gh secret delete NAMEImmediately remove a repository secret
gh variable set NAME --body valueStore a non-sensitive value that workflows can read
gh variable get NAMEPrint a variable value
gh variable listList variables with their values

Keep any dotenv file used with --env-file out of version control, and delete it once the values are uploaded. Before deleting a secret, confirm the repository and secret name because GitHub cannot return the stored value afterward.

Releases

Tag, publish, and fetch release assets.

CommandDescription
gh release create TAG --generate-notesCreate a release with notes built from merged pull requests
gh release create TAG --target main --title "..."Create the tag on a specific branch or commit
gh release create TAG dist/app dist/checksums.txtAttach build files while creating the release
gh release create TAG --draft --prereleasePublish as a draft or a prerelease
gh release listList releases
gh release view TAGShow the notes and assets for one release
gh release download TAGDownload every asset
gh release download TAG --pattern "*.tar.gz"Download matching assets only
gh release upload TAG fileAdd an asset to an existing release
gh release delete TAGDelete a release

Search and Browse

Find work across GitHub, then jump to it.

CommandDescription
gh search repos QUERY --language goSearch repositories
gh search issues QUERY --state openSearch issues
gh search prs --review-requested @meSearch pull requests
gh search code QUERYSearch code
gh search commits QUERYSearch commit messages
gh browseOpen the current repository in a browser
gh browse NUMBEROpen an issue or pull request
gh browse path/to/file.goOpen a file at the current branch
gh browse --actionsOpen the Actions tab
gh browse -n path/to/filePrint the URL instead of opening it
gh statusShow issues, pull requests, and notifications across repositories

JSON Output and the API

Structured output for scripts, and direct API access for everything else.

CommandDescription
gh pr list --json number,title,authorReturn selected fields as JSON
gh pr list --json number,title --jq '.[].title'Filter JSON without a separate jq process
gh run list --json conclusion --template ...Format JSON with a Go template
gh help formattingSee every JSON formatting option
gh pr list --jsonPassing no value lists the fields that command exposes
gh api repos/{owner}/{repo}/releasesSend an authenticated REST request
gh api ENDPOINT --jq '.[].tag_name'Filter the API response
gh api ENDPOINT -f title="..." -f body="..."Send a POST with string fields
gh api ENDPOINT -X PATCH -F draft=falseChoose the method and send typed fields
gh api ENDPOINT --paginateFollow pagination and return every page
gh api graphql -f query='...'Send a GraphQL query

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

Aliases, Extensions, and Config

Shorten what you type, and add commands gh does not ship with.

CommandDescription
gh alias set pv 'pr view --web'Create a shortcut
gh alias set bugs 'issue list --label bug'Alias a command with its flags
gh alias listList configured aliases
gh alias delete NAMERemove an alias
gh extension search QUERYFind extensions
gh extension install OWNER/gh-NAMEInstall an extension
gh extension listList installed extensions
gh extension upgrade --allUpdate every installed extension
gh extension remove NAMERemove an extension
gh config set editor vimSet the editor gh opens for bodies and prompts
gh config set git_protocol sshChoose HTTPS or SSH for Git operations
gh config listShow current configuration

Extensions are not reviewed or signed by GitHub, and they run with your token. Read the source before installing one.

Troubleshooting

What the common failures mean and where to look.

MessageCheck
To get started with GitHub CLI, please run: gh auth loginNo credentials for this host; run gh auth login, or set GH_TOKEN in CI
HTTP 403: Resource not accessible by personal access tokenCheck with gh auth status; refresh OAuth scopes, or fix fine-grained token access, SSO authorization, or Actions permissions
error: your authentication token is missing required scopes [workflow]Refresh with the scope named in the message
none of the git remotes configured for this repository point to a known GitHub hostRun inside a GitHub clone, pass --repo OWNER/REPO, or run gh repo set-default
multiple remotes detected. please select which repo to useSet the target once with gh repo set-default
pull request create failed: GraphQL: No commits between main and branchCommit and push the branch before creating the pull request
HTTP 404: Not Found on a private repositoryThe account is authenticated but lacks access, or SSO is not authorized for the token
X status with no logs in gh run viewLogs expire with the retention policy; check the run age

Deeper reading on GitHub CLI and the Git commands beside it.

GuideDescription
GitHub CLI: Manage Repositories, Issues, and Pull RequestsFull gh walkthrough with examples and troubleshooting
git clone CommandClone repositories directly with Git
git diff CommandRead the patch format gh pr diff prints
How to Add Git RemotesManage the remotes gh repo create configures
gitignore: Ignoring Files in GitKeep secrets and build output out of a repository
Git cheatsheetQuick reference for the Git commands themselves