Sub topic 6
Reviewing, checking status, and merging
This chapter covers the last stretch of a pull request: reading it, reviewing it, confirming CI is green, and merging it. It assumes you can already open a pull request with gh pr create, as covered in the previous chapter, and that you are signed in with gh auth login.
Reading a pull request
Start by seeing what the pull request changes. Pass a number, a URL, or a branch name. With no argument, gh uses the pull request for your current branch.
gh pr view 42
gh pr diff 42
gh pr checkout 42gh pr view prints the title, description, and review state. gh pr diff prints the patch in your terminal. gh pr checkout fetches the branch so you can run the code locally.
Submitting a review
gh pr review records a review with one of three outcomes.
gh pr review 42 --approve --body "Looks good."
gh pr review 42 --request-changes --body "Please add a test for the empty config case."
gh pr review 42 --comment --body "Question on the retry logic."If you leave out the flags, gh asks you which kind of review you want. For a single note without a verdict, gh pr comment 42 --body "..." is enough.
Checking status
Before merging, confirm that CI passed. gh pr checks lists each check and its result. gh pr status summarizes your pull requests, including review and check state.
gh pr checks 42
gh pr checks 42 --watch
gh pr status--watch keeps refreshing until all checks finish. The exit code is non-zero when a check fails, so you can use the command in scripts.
Merging
gh pr merge supports the three merge methods GitHub offers. You pick one with a flag.
| Flag | Result |
|---|---|
| `--merge` | Creates a merge commit |
| `--squash` | Combines all commits into one |
| `--rebase` | Replays commits onto the base branch |
gh pr merge --squash --delete-branch--delete-branch removes the branch after the merge. If you give no method flag, gh prompts for one. In a script, always pass a method, or the command waits for input.
Merging when checks are not done
Branch protection rules can block a merge until required checks pass and reviews are approved. Two flags help here.
--autoenables auto-merge. GitHub merges the pull request when all requirements are met. The repository must have auto-merge turned on.--adminbypasses requirements. It needs admin permission, so use it sparingly.
gh pr merge 42 --squash --autoCommon errors
- Wrong pull request. Without a number,
ghuses the current branch. Check withgit branch --show-currentfirst. - Merge blocked. Run
gh pr checksandgh pr viewto find the failing check or missing approval. - Cannot approve your own pull request. GitHub rejects
--approvefrom the pull request's author. Use--commentinstead. - Prompt in automation. Missing method flags cause an interactive prompt. Add
--squash,--merge, or--rebase.
Exercise
You want a pull request merged as a single commit as soon as its required checks pass, without waiting at the terminal. Which command do you run, and what must be true of the repository?
Show answer
Run gh pr merge 42 --squash --auto. The repository must have auto-merge enabled, and GitHub then merges the pull request once all required checks and reviews are satisfied.
Exercise
A teammate's pull request shows a failing check. Which two commands help you see its state and then follow it until it finishes?
Show answer
gh pr checks 42 shows each check and its result. gh pr checks 42 --watch keeps refreshing until all checks complete.