Building it · lesson 5 of 10
Ideas in this lesson
Agit push -u origin branchsend a branch to the shared copy
BPull requesta proposal to merge, with a place to discuss it
CCode reviewa teammate reads the change before it’s merged
Dgit pullbring the shared copy’s new commits down

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.

Working with a remote
CommandWhat it does
git push -u origin how-to-runSends the branch’s commits to origin; -u remembers where, so next time git push is enough
git pullBrings origin’s new commits down and merges them into your branch
git fetchBrings 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.

Git sandbox
  1. Make a branch how-to-run
  2. Add a ## How to run section to README.md, and commit it
  3. Push the branch with an upstream
  4. Open a pull request from it on the hosting site, with a clear title
  5. When it’s approved, merge it
  6. 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

  1. 6c3d6f9 HEAD →mainorigin/main Give out numbers, one per phone, up to 60 a day
  2. 389ecdd Start 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:

  1. Does it work? Does it do what the story needs, including the edge cases?
  2. Tests: are there tests, and would they fail if the code were wrong?
  3. Design and complexity: does it fit the design, and could it be simpler?
  4. Security and privacy: does it trust input it shouldn't, or expose data (lesson 4.7)?
  5. Naming and comments: could a new teammate understand it?
  6. 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.

queue_rules.py
sms.py

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

  1. This raises KeyError if the phone has no number. Could cancel() return None, so the reply can say so?

  2. This is wrong.

  3. Why would anyone write it like this?

  4. Nit: n could be number, to match queue_rules.py. Not blocking.

  5. I would have used a different approach.

  6. 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

Spot the trapMarco

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

What is a pull request?
Which review comment is most useful?
Why should pull requests be small?

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.

All sources for Simple Software Development