Pull requests and code review
Last updated
Before a change reaches main, someone else reads it. That second pair of eyes catches bugs, spreads knowledge of the code around the team, and keeps it readable. In this lesson you'll push a branch to Pila's shared repository, open a pull request, and then review one of Marco's, looking for the problems a good reviewer finds.
By the end of this lesson you can
- Push a branch to a shared repository and open a pull request
- Review a change for correctness, tests, clarity and security
- Write review comments that are specific, kind and about the code
- Explain why small pull requests get better reviews
Remotes, push and pull
Pila's team shares one copy of the repository on a hosting site (lesson 5.2). In your clone, that copy is a remote called origin.
| Command | What it does |
|---|---|
git push -u origin how-to-run | Sends the branch’s commits to origin; -u remembers where, so next time git push is enough |
git pull | Brings origin’s new commits down and merges them into your branch |
git fetch | Brings new commits down without merging, so you can look first |
The team's rule: nobody pushes straight to main. Changes arrive by pull request, and the hosting site can enforce it (Protecting branches in Simple Git). For more on remotes, see Simple Git's Remotes and cloning, Fetch, pull and push and When push is rejected.
Open a pull request
A pull request (GitLab calls it a merge request) is a page on the hosting site that proposes merging one branch into another. It shows the changes, and gives the team a place to discuss them before anything is merged.
The README doesn't say how to run Pila. Fix it on a branch, push it, and propose it. Type edit README.md to change the file; add a section starting ## How to run.
- Make a branch
how-to-run - Add a
## How to runsection to README.md, and commit it - Push the branch with an upstream
- Open a pull request from it on the hosting site, with a clear title
- When it’s approved, merge it
- Switch to main and pull, so your main has the merge
Working folder
no changes
Staging area
nothing staged
Repository
2 commits on main
Remote
origin: example.com/pila-team/pila.git · in step, as of your last fetch
You are in ~/pila, your clone of the team’s repository (https://example.com/pila-team/pila.git), on main.
Commit graph
6c3d6f9HEAD →mainorigin/main Give out numbers, one per phone, up to 60 a day389ecddStart Pila with a README
On the hosting site: example.com/pila-team/pila
Branches: main
A good pull request description says what changed, why (link the story or bug), and how the reviewer can check it. Simple Git's Pull requests, Reviewing on GitHub and Merging on GitHub walk through the same steps on GitHub itself, with screenshots.
What reviewers look for
Code review is reading someone else's change before it's merged, looking for problems and asking questions. Google's published review guidelines list what to look for, most important first:
- Does it work? Does it do what the story needs, including the edge cases?
- Tests: are there tests, and would they fail if the code were wrong?
- Design and complexity: does it fit the design, and could it be simpler?
- Security and privacy: does it trust input it shouldn't, or expose data (lesson 4.7)?
- Naming and comments: could a new teammate understand it?
- Style: but leave that to tools (lesson 5.6).
Review also spreads knowledge: after reviewing Marco's cancel feature, you can fix it when he's away.
Review Marco’s pull request
Marco has opened a pull request for cancelling by text. There are four real problems in it. Click each line you'd comment on, then finish the review.
Pull request · Marco wants to merge into main
Let patients cancel their number by text
Adds Queue.cancel() and handles the CANCEL text in sms.py. Story: “As a patient, I want to cancel my number, so that the next person is called sooner.”
Read the change. Click any line you would comment on, then press Finish review.
None of these would be caught by trying it once: they show up on the second patient, the lowercase text, or the privacy audit. Reading slowly, line by line, finds them.
Comments that help
How you say it matters as much as what you find. Helpful comments are about the code, not the person; say why; suggest rather than order; and mark small optional points as nits. Which of these comments help?
0 of 6 placed right
This raises KeyError if the phone has no number. Could cancel() return None, so the reply can say so?
This is wrong.
Why would anyone write it like this?
Nit:
ncould benumber, to match queue_rules.py. Not blocking.I would have used a different approach.
Nice: the reply fits in one text message, so it costs one credit.
Authors have a part too: keep pull requests small. A reviewer reads 100 lines carefully and 2,000 lines barely at all, which is why lesson 5.1's one-day tasks matter.
Spot the trap: approving by the title
Marco opens a pull request on Friday afternoon: “Refactor queue and storage”, 2,000 lines across 31 files. “It’s just tidying. Can someone approve so I can merge before the weekend?”
What happens if…
Check what you learned
Sources
Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.
- GitHub Docs: About pull requests: proposing, discussing and merging changes
- Google Engineering Practices: What to look for in a code review: design, functionality, complexity, tests, naming, comments
- Google Engineering Practices: How to write code review comments: be kind, explain why, label nits
- Google Engineering Practices: Small CLs: small changes are reviewed faster and more thoroughly
- Pro Git, 2.5: Working with Remotes: push, fetch and pull