Pull requests and code review
In professional teams, nobody pushes straight to main. Changes go through a Pull Request (PR), called a Merge Request on GitLab. It's a proposal to merge your branch, where teammates review the code and automated checks run before anything reaches production.
The PR workflow
1. git switch -c feature/x branch off an up-to-date main
2. commit, commit, commit small focused commits
3. git push -u origin feature/x publish the branch
4. Open the PR on GitHub describe what and why
5. CI runs tests + reviewers comment
6. Push more commits to address feedback (the PR updates automatically)
7. Approved + green checks → Merge
8. Delete the branch; pull main locallybash
git switch main && git pull
git switch -c feature/price-filter
# ...work...
git push -u origin feature/price-filter
gh pr create --fill # GitHub CLI: open a PR from the terminalWriting a great PR description
markdown
## What
Adds a price range filter to the product search page.
## Why
Customers asked for it (#212). 30% of searches are followed by sorting by price.
## How
- New `PriceRangeSlider` component
- `/api/products` accepts `minPrice` and `maxPrice` query params
- Index added on `products.price`
## Screenshots
| Before | After |
| --- | --- |
|  |  |
## Testing
- [x] Unit tests for the query builder
- [x] Tested manually on mobile Safari and Chrome
Closes #212Writing Closes #212 (or Fixes #212) automatically closes the linked issue when the PR merges.
Keep PRs small
| PR size | What reviewers do |
|---|---|
| ~50–200 lines | Read carefully and find real bugs |
| 1,000+ lines | Skim it, write "LGTM 👍", and miss the bugs |
Split big features into a series of small PRs. Feature flags let unfinished work merge safely while it stays hidden from users.
Reviewing code (being a good reviewer)
Look for, in this order:
- Correctness: does it do what it says? Are edge cases (empty, null, huge input) handled?
- Security: user input is validated, there are no secrets, and authorization is checked.
- Design: is it in the right place? Is it readable? Is it consistent with the codebase?
- Tests: are important paths covered?
- Style: leave this to automated linters and formatters, not humans.
How to comment:
- Ask questions: "What happens if
itemsis empty here?" - Label how important a comment is: nit: (minor), suggestion:, blocking:
- Praise good work too 🙌
- Review the code, not the person
GitHub's suggestion feature lets the author apply your fix with one click:
markdown
```suggestion
const total = items.reduce((sum, i) => sum + i.price, 0);
```Receiving feedback
- Don't take it personally. Reviews make the code (and you) better.
- Reply to every comment: fix it, or explain why not.
- Push follow-up commits; avoid force-pushing during review so reviewers can see what changed.
Merge strategies on GitHub
| Button | Result | Good for |
|---|---|---|
| Create a merge commit | Keeps all commits + a merge commit | Preserving full history |
| Squash and merge | All PR commits become one commit on main | Clean linear history (most popular) |
| Rebase and merge | Replays each commit onto main, with no merge commit | Linear history that keeps individual commits |
Protecting main
Repository admins set branch protection rules (GitHub → Settings → Branches):
- Require a pull request before merging
- Require 1–2 approving reviews
- Require status checks (CI tests, lint) to pass
- Require the branch to be up to date with main
- Block force-pushes and deletions
- CODEOWNERS file: automatically request reviews from the owners of the files that changed
bash
# .github/CODEOWNERS
/src/payments/ @payments-team
*.sql @db-adminsPractice (real GitHub)
- In a repository of yours, enable branch protection on
main, requiring a PR. - Create a branch, make a small change, push it, and open a PR with a proper description.
- Review your own PR using GitHub's "Files changed" tab: leave a comment and a suggestion, apply the suggestion, then Squash and merge.
- Pull
mainlocally and delete the branch (git branch -d …).
✎ Check your understanding
1.Why are small pull requests better?
2.What does "Squash and merge" do?