GitHub CLI
Manage PRs, issues and runs from the terminal.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 9 minutes to read.
In short
The GitHub CLI, gh, brings pull requests, issues and Actions runs to your terminal. It saves context switching and makes GitHub tasks easy to script.
Explained simply
gh is GitHub's official command-line tool. Git handles commits and branches; gh handles the GitHub side: pull requests, issues, workflow runs, releases and repo settings. You can do your whole review-and-merge loop without opening a browser.
You sign in once with gh auth login. It opens the browser, you approve, and gh stores a token safely (in your system's keyring when one is available). From then on, gh knows which account you are and can also act as the credential helper for git push over HTTPS.
gh works out which repo you mean from the folder you are in, by looking at your git remotes. Run it inside a clone and gh pr list shows that repo's pull requests. Use -R owner/repo to point at any other repo.
The most powerful part is scripting. Almost every command can print JSON with --json and filter it with --jq. gh api can call any GitHub API endpoint with your login, so anything the website can do, a script can do too.
Press Play on a diagram to walk through it one step at a time.
- 1gh auth login
Authenticate once through the browser or a token.
- 2gh pr create
Open a PR from the current branch.
- 3gh pr checks
See CI status for the PR.
- 4gh run watch
Follow a workflow run live in the terminal.
- 5gh pr merge
Merge with squash or rebase and delete the branch.
- auth
- login
- status
- refresh -s scope
- pr
- create
- checkout
- review
- merge
- checks
- issue
- create
- list
- close
- run and workflow
- run list
- run watch
- run view --log-failed
- workflow run
- repo and release
- repo clone
- repo fork
- release create
- api
- REST calls
- graphql
- --paginate
In depth
Installing and authenticating
Install with your package manager: brew install gh on macOS, winget install --id GitHub.cli on Windows, and the official apt or dnf repositories from cli.github.com on Linux. Then run gh auth login, pick GitHub.com, HTTPS, and log in with the browser. gh auth status shows which account and scopes you have.
Some features need extra scopes. Projects need the project scope: gh auth refresh -s project. In scripts and CI, gh skips the login and reads a token from the GH_TOKEN environment variable (GITHUB_TOKEN also works). gh is preinstalled on GitHub-hosted runners, so a workflow step only needs env: GH_TOKEN: ${{ github.token }}.
- gh auth setup-git makes git use gh for HTTPS credentials
- gh auth switch changes between logged-in accounts
- GH_REPO=owner/repo sets the target repo for every command
The pull request workflow from the terminal
gh pr create --fill uses your commit messages for the title and body; add --draft, --base, --reviewer and --label as needed. gh pr status shows your PRs and the ones waiting for your review. gh pr view opens the details, --comments shows the discussion, and --web opens the browser.
To review someone else's work, gh pr checkout 123 fetches the branch (even from a fork) and switches to it. gh pr diff shows the changes. gh pr review 123 --approve, --comment or --request-changes with -b submits a review. To finish, gh pr merge with --squash, --merge or --rebase, plus --delete-branch. Add --auto to merge automatically once required checks and reviews pass.
- You to gh: gh pr list --search review-requested:@me1gh pr list --search review-requested:@me
- gh to GitHub: Search API query2Search API query
- You to gh: gh pr checkout 1233gh pr checkout 123
- gh to GitHub: Fetch PR branch, even from a fork4Fetch PR branch, even from a fork
- You to gh: gh pr review 123 --approve5gh pr review 123 --approve
- gh to GitHub: Submit review6Submit review
Working with Actions runs
gh run list shows recent workflow runs with their status; filter with --workflow ci.yml, --branch or --status failure. gh run watch follows a run live, and --exit-status makes the command fail if the run fails, which is handy in scripts. gh run view ID --log-failed prints only the logs of failed steps, usually exactly what you need.
gh run rerun ID --failed retries just the failed jobs, and gh run download ID -n dist fetches an artifact. For workflows with a workflow_dispatch trigger, gh workflow run deploy.yml -f environment=staging starts a run with inputs, and --ref chooses the branch. gh workflow disable and enable pause a noisy scheduled workflow.
gh api, JSON output and extensions
gh api repos/{owner}/{repo}/pulls calls the REST API with your token; {owner} and {repo} are filled from the current repo. Use -X for the method, -f for string fields and -F for typed ones (numbers, booleans, @file). --paginate follows every page, and --jq filters the result. gh api graphql -f query='...' runs GraphQL queries.
For normal commands, --json lists the fields you want and --jq or --template shapes them, so you never scrape human output. gh alias set lets you save long commands under a short name. gh extension install adds community commands, for example gh extension install dlvhdr/gh-dash for a terminal dashboard of PRs and issues.
- gh pr list --json number,title,author --jq '.[] | "\(.number) \(.title)"'
- gh alias set mine 'pr list --author @me'
- gh api rate_limit shows how many API calls you have left
Learn it in 3 short steps
About 30m each. Track them with a timer in your plan.
- 1Install gh and authenticate~30m
- 2Create and check out PRs from the terminal~30m
- 3Watch workflow runs with gh run~30m
Key points
- gh pr checkout 123 fetches a teammate's PR branch so you can test it locally.
- gh run list and gh run view --log-failed find why CI broke.
- gh api calls any GitHub REST or GraphQL endpoint with your login.
- Most commands accept --json for output you can pipe into scripts.
Key terms
Tap a card to flip it. 0/4 known
Examples
gh auth login
gh pr create --fill --base main
gh pr checks --watch
gh pr checkout 123
gh run list --limit 5
gh run view --log-failed
gh pr merge --squash --delete-branch
gh pr list --json number,title --jq '.[].title'Day-to-day PR and CI work from the terminal.
Debug a red CI run in under a minute
Your PR shows a red cross. Step 1: list the checks on the PR to see which job failed. Step 2: list the latest failed runs on your branch. Step 3: print only the failed step's logs. Step 4: fix, push, and watch the new run until it finishes. If the failure was a flaky network call, rerun only the failed jobs instead of the whole run.
gh pr checks
gh run list --branch "$(git branch --show-current)" --status failure --limit 3
gh run view 9876543210 --log-failed
# ...fix the bug, commit, push...
gh run watch --exit-status
# flaky? retry only what failed:
gh run rerun 9876543210 --failedA script that finds stale pull requests
Your team lead wants a weekly list of open PRs not updated for 14 days. Step 1: ask gh for open PRs with the fields you need as JSON. Step 2: use a jq filter to compare updatedAt with a cutoff date. Step 3: print number, author and title. Run it from cron or a scheduled workflow with GH_TOKEN set.
cutoff=$(date -u -d '14 days ago' +%Y-%m-%dT%H:%M:%SZ) # GNU date
gh pr list --state open --limit 200 \
--json number,title,author,updatedAt \
--jq ".[] | select(.updatedAt < \"$cutoff\") | \"#\(.number) @\(.author.login) \(.title)\""Quick reference
- gh auth status shows the account, token source and scopes in use
- gh pr create --fill --draft opens a draft PR from your commits
- gh pr checkout 123 fetches and switches to PR 123, even from a fork
- gh pr merge --squash --delete-branch --auto merges once everything passes
- gh run view ID --log-failed prints only the failed steps' logs
- gh workflow run deploy.yml -f environment=staging starts a workflow_dispatch run
- gh api repos/{owner}/{repo}/pulls --paginate --jq '.[].title' calls the REST API
- In Actions, set env GH_TOKEN: ${{ github.token }} before using gh
Common mistakes
- Running gh in the wrong directory, so it targets a different repo.
- Pasting tokens into scripts instead of relying on gh auth.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Install gh, log in, and list the five most recent issues and pull requests in a repo you use.
- Exercise 2medium
Create an alias called review that lists PRs waiting for your review, and another called mine that lists your open PRs with their check status.
- Exercise 3hard
Write a workflow step that comments the test coverage number on the current pull request using gh, with the least permissions needed.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is gh the same as git?
- No. git manages your local repository and talks to any Git server. gh talks to GitHub's API for pull requests, issues, Actions and settings; you use both together.
- Does gh work with GitHub Enterprise Server?
- Yes. Log in with gh auth login --hostname your.github.host and gh will use that server for repos cloned from it.
- Where does gh store my token?
- In your operating system's credential store when one is available, otherwise in a config file under ~/.config/gh. gh auth token prints the token if a script needs it.