Pull Requests and Code Review with the GitHub CLI
You will open a pull request from the command line, run reviews, and merge only after checks pass. By the end you will have a review workflow and branch protection that keeps unreviewed code out of main.
Learning Objectives
- Create and manage PRs with the gh CLI.
- Request reviews and respond to feedback.
- Require status checks and reviews before merge.
- Time: ~2 hours · Difficulty: Intermediate · Prereqs: a GitHub repo and the gh CLI.
Architecture Overview
graph LR
B[feature branch] -->|gh pr create| PR[Pull Request]
PR --> CI[Required checks]
PR --> Rev[Review approval]
CI -->|pass| Merge{Merge allowed}
Rev -->|approved| Merge
Merge --> Main[main]
Environment Setup
You will need: the gh CLI authenticated (gh auth login) and a repo.
Before you begin: enable branch protection on main in repo settings.
Step-by-Step Execution
01
Open a pull request
gh pr create --base main --head feature/api --title "Add API" --body "Implements endpoint"02
Review and check status
$ gh pr checks && gh pr review --approve
All checks were successful
Approved pull request #42
03
Merge after checks pass
gh pr merge --squash --delete-branchProgress So Far
graph LR
A[01 Open PR] -->|done| B[02 Review + checks]
B -->|done| C[03 Merge]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
gh pr list --state merged --limit 1 && git log main --oneline -1You should see the merged PR and the squashed commit on main. With protection on, an unreviewed or failing PR cannot merge.
Troubleshooting
- Merge button blocked: branch protection requires checks/reviews; satisfy them first.
- gh not authenticated: run
gh auth loginand select the right account. - Stale branch after merge: use
--delete-branchto keep the repo tidy.
Extension Ideas
- Add automated checks in GitHub Actions.
- Require CODEOWNERS review for sensitive paths.
- Auto-merge once checks pass with
gh pr merge --auto.
Key Results
- Opened and merged a PR entirely from the CLI.
- Enforced review approval before merge.
- Required passing checks via branch protection.
- Kept main clean with squash + branch deletion.