Pull Requests & Code Reviews
A complete guide to Pull Requests (PRs): code review etiquette, branch protection rules, atomic commits, and automated CI gates.
In the early days of open-source software, contributing code to a project often meant generating a patch file with the `diff` command and emailing it to a project mailing list. The project maintainer had to download the patch, inspect the code in a text editor, test it on their machine, and manually apply it. In 2008, GitHub introduced the Pull Request (called a Merge Request in GitLab). A Pull Request (PR) is a dedicated collaboration space where a developer asks teammates to review and approve changes before merging them into the main codebase. It provides side-by-side code diffs, line-by-line comments, automated test results, and discussion threads in one place.
Definition
A Pull Request (PR) is a collaborative review process in modern version-control platforms where a developer proposes changes from a feature branch to a target branch, allowing teammates to review code, run automated tests, and discuss improvements before merging.
Why It Matters
Pull Requests are one of the most important quality and security checkpoints in software development. Without PRs, anyone could push untested or insecure code straight to production. Peer code reviews catch the majority of bugs and edge-case errors before software ever reaches customers. Just as importantly, PRs serve as a learning tool: newer developers learn architectural patterns from experienced colleagues, and the whole team maintains shared awareness of changes across the project.
How It Works
An effective Pull Request process follows four structured stages: 1. Author Preparation: • Focused Commits: Group related changes together with clear commit messages using conventions like `feat:`, `fix:`, or `docs:`. • Manageable Scope: Keep pull requests reasonably small (ideally under 300 to 400 lines of code). Smaller PRs are reviewed faster and receive much more thorough feedback. • Helpful Context: Provide a brief summary of what changed, why the change was made, and link to the relevant project issue or ticket. 2. Automated CI Checks (Machine Review First): Before a teammate spends time reading the code, automated pipelines verify the basics: linters check formatting, type checkers validate types, and automated unit tests verify that functionality is intact. If automated checks fail, the author fixes them before requesting review. 3. Peer Review: Teammates review architectural choices, edge cases, error handling, and readability. Reviewers focus on constructive feedback: discussing the code rather than criticizing the author, and explaining the reasoning behind suggestions. 4. Branch Protection and Merging: Repository settings enforce safety rules: requiring at least one approved review, green CI test results, no merge conflicts, and designated sign-offs from key domain owners using a CODEOWNERS file.
The Pull Request Review and Verification Flow
Key Points
- Automate the routine checks: linters and formatters should run in CI so humans do not spend time debating spacing or syntax.
- Keep PRs small: smaller pull requests are reviewed much faster and catch significantly more bugs.
- Branch protection rules ensure that unreviewed or failing code cannot be pushed directly to main.
- Focus feedback on the code and system design, explaining the reasons behind suggestions.
- Use CODEOWNERS to ensure sensitive areas (like authentication, payments, or infrastructure) require review from relevant experts.
Steps
Write Focused, Clear Commits
Use clear commit messages and group related changes together so the PR history is straightforward to follow.
Add Context in the PR Description
Briefly summarize what was built, why the approach was chosen, and how you tested the changes.
Verify That All Automated Checks Pass
Check that linting, unit tests, and build checks are green before asking colleagues for their time.
Participate in Constructive Reviews
Reviewers should frame feedback clearly with suggested alternatives; authors should treat suggestions as collaborative improvements.
Merge and Clean Up
Merge the approved pull request using squash-and-merge or rebase, and delete the feature branch to keep the repository tidy.
Examples
The Architectural Blueprint Analogy
In building construction, an architect does not start pouring concrete without review. They submit a blueprint (Pull Request). Automated software checks building code dimensions (CI), and structural and electrical inspectors (Peer Reviewers) examine safety calculations before signing off on the permit.
Real-World Examples
Standardized Code Reviews at Tech Organizations
Large technology companies require that code changes be approved by at least one peer who understands the feature and an authorized owner who verifies language standards. Automated review platforms enforce these rules before code can be merged into production.
Technical Examples
# ─── File 1: .github/CODEOWNERS ──────────────────────────────────────
# Automatically assign reviewers based on the folders modified in a PR
# Default reviewers for all files in the repository
* @myorg/core-team
# Security and authentication require sign-off from the security team
/src/auth/ @alice @myorg/security-team
/src/crypto/ @alice @myorg/security-team
# Infrastructure changes require approval from the platform team
/terraform/ @bob @myorg/platform-team
/.github/workflows/ @bob @myorg/platform-team
# Database schema files require DBA review
/prisma/schema.prisma @carol @myorg/dba-team
# ─── File 2: .github/pull_request_template.md ─────────────────────────
## Summary of Changes
- Describe the problem this PR solves and the approach taken.
## Related Issues
- Closes #[issue-number]
## Type of Change
- [ ] Bug fix (non-breaking fix)
- [ ] New feature (adds functionality)
- [ ] Refactoring / performance improvement
## Checklist
- [ ] Added or updated automated unit tests
- [ ] Verified locally that existing functionality continues to work
- [ ] Checked that all automated CI checks passSmall, Focused PR (< 300 lines) vs Large 'Mega PR' (1,000+ lines)
| Aspect | Small, Focused PR (< 300 lines) | Large 'Mega PR' (1,000+ lines) |
|---|---|---|
| Review Time | 15 to 30 minutes; reviewed promptly | Often delayed for days because it feels overwhelming |
| Defect Catch Rate | High: reviewers can inspect logic carefully | Low: reviewers tend to skim and miss subtle bugs |
| Merge Conflict Risk | Low, because it merges quickly | High, accumulating conflicts as time passes |
| Ease of Rollback | Simple: reverses a single isolated change | Difficult: undoing it reverts multiple unrelated features |
Constructive Review Etiquette
Advantages
- Defect Prevention: Catches logic mistakes, security concerns, and performance issues before code reaches users.
- Shared Knowledge: Helps teammates understand other parts of the codebase, preventing single points of failure.
- Consistent Quality: Maintains steady standards for code style, structure, and testing.
Limitations
- Review Delays: If reviewers take days to look at pull requests, development velocity slows down.
- Over-Debating Trivial Details: Teams can waste time debating formatting unless automated linters are used to settle style rules.
Interview Questions
What are Branch Protection Rules and why are they important?
Branch Protection Rules are repository settings applied to key branches (like `main`). They prevent developers from pushing code directly without a pull request, require passing automated CI tests and security scans, and enforce minimum review approvals from teammates or designated CODEOWNERS before code can be merged.
Practice Questions
- Add a `.github/pull_request_template.md` file to a repository to help authors provide clear descriptions of their changes.
- Turn on branch protection on the `main` branch of a GitHub repository, requiring at least one review approval before merging.
