Sub topic 5
Opening and Checking Out Pull Requests
This chapter assumes you can use Git (branch, commit, push), that gh is already authenticated (see the chapter on logging in), and that you know what a pull request is on GitHub. You will open a pull request from a branch, list pull requests, and check out someone else's pull request locally.
The working model
A pull request (PR) in gh is always tied to a head branch (your changes) and a base branch (where they should land). Most gh pr commands work on the PR for your current branch unless you pass a number, URL, or branch name.
Opening a pull request
Create a branch, push it, and open the PR:
git switch -c fix-empty-config
git push -u origin fix-empty-config
gh pr create --fill --base main
gh pr checks--fill takes the title and body from your commit messages. With one commit, the title is the commit subject and the body is the commit body. With several commits, the title comes from the branch name and the body lists the commit subjects. --base main names the branch the PR targets. gh pr checks then reports the status of the PR's CI checks.
If you leave out --fill, gh pr create asks for a title and body interactively. To supply them directly, use flags:
gh pr create --title "Handle empty config" --body "Fixes the crash when the config file is empty." --draft--draft opens the PR as a draft. Other useful flags are --reviewer, --assignee, --label, and --web, which opens the prefilled form in your browser instead.
Listing and viewing pull requests
gh pr list
gh pr list --state all --author @me
gh pr view 123
gh pr view 123 --webgh pr list shows open PRs by default. --state accepts open, closed, merged, or all. @me stands for the signed-in user. gh pr view prints the title, body, and status. With no argument, it shows the PR for your current branch.
Checking out a pull request
To test someone else's changes, check the PR out:
gh pr checkout 123gh fetches the PR's head, creates a local branch, and switches to it. This works for PRs from forks too, and it sets up tracking so git pull brings in new commits the author pushes later. If you want a different local branch name, pass --branch:
gh pr checkout 123 --branch review-123To see what changed without checking out, use gh pr diff 123.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no pull requests found for branch` | The current branch has no PR | Run `gh pr create`, or pass a PR number |
| `head branch is the same as base branch` | You ran `gh pr create` while on `main` | Switch to a feature branch first |
| `a pull request for branch ... already exists` | A PR is already open for this head and base | Use `gh pr view` to find it |
| Checkout fails with local changes | Uncommitted edits conflict with the PR files | Commit or stash, then retry |
| Wrong repository targeted | The clone has several remotes | Pass `--repo owner/name` or set a default with `gh repo set-default` |
Exercise
You are on branch fix-empty-config, which you have pushed. Which command opens a draft pull request against main using your commit messages for the title and body?
Show answer
gh pr create --fill --base main --draft
Exercise
A colleague's PR number is 87 and comes from a fork. How do you test it locally on a branch named try-87?
Show answer
gh pr checkout 87 --branch try-87. gh fetches the PR head from the fork, creates the local branch, and switches to it.