Sub topic 15
Pull requests from the command line with gh
This chapter assumes you know basic Git (commit, branch, push), have a GitHub account, and understand what a pull request is and how reviews work in the web interface. Here you will do the same work from a terminal with gh, the GitHub CLI.
Why use gh
gh talks to the GitHub API, so you can open, check out, review, and merge pull requests without leaving your terminal. It saves context switches, and it can be scripted. Install it from cli.github.com, then sign in once with gh auth login.
Create a pull request
Start from a pushed feature branch, then open the pull request against main.
git switch -c fix-login-timeout
# edit files, then:
git add .
git commit -m "Increase login request timeout to 30s"
git push -u origin fix-login-timeout
gh pr create --base main --title "Increase login request timeout" --body "Raises the timeout from 10s to 30s to stop spurious failures on slow networks."
gh pr create --draft --fillThe first gh pr create sets the title and body explicitly. The second opens a draft pull request, which signals that the work is not ready for review. --fill takes the title and body from your commits. Run gh pr create with no flags and it prompts you for each field.
Check out, inspect, and review
To test someone else's change, check it out locally, look at its checks, and submit a verdict.
gh pr checkout 123
gh pr checks 123
gh pr review 123 --request-changes --body "Please add a test for the timeout case."
gh pr review 123 --approve
gh pr merge 123 --squash --delete-branchgh pr checkout 123fetches the pull request's branch and switches to it. This works for forks too.gh pr checks 123lists each status check and whether it passed, failed, or is pending.gh pr reviewsubmits one of the three outcomes:--comment,--approve, or--request-changes. A body is required for comments and change requests.gh pr mergeapplies a merge strategy:--merge,--squash, or--rebase.--delete-branchremoves the branch afterward.
The last two review commands are alternatives. Run the one that matches your verdict, not both.
A typical flow
The diagram below shows the commands in the order you would normally use them.
Common errors
- No upstream branch. If
gh pr createcomplains that the branch is not pushed, rungit push -u origin <branch>first. - Merge blocked.
gh pr mergefails when branch protection requires reviews or passing checks. Rungh pr checksto see what is missing. - Wrong base branch. Without
--base, the pull request targets the repository's default branch. Pass--basewhen you target a release branch. - Not signed in. Commands fail with authentication errors until
gh auth loginsucceeds.gh auth statusshows the current state.
Exercises
Exercise
You finished a branch but want feedback before it is ready to merge. Which gh command opens the pull request, taking its title and body from your commits?
Show answer
gh pr create --draft --fill. The --draft flag marks it as not ready for review, and --fill uses the commit messages for the title and body.
Exercise
gh pr merge 123 --squash is rejected. Which command shows why, and what are two likely causes?
Show answer
gh pr checks 123 shows the status of each check. Likely causes are a failing or pending required check, or a missing required approval under branch protection.