Code Review and Pull Request Etiquette
Code review is the practice of having at least one other person read a change before it is merged. On GitHub, GitLab and Bitbucket, it happens in a pull request: reviewers read the diff, ask questions, suggest improvements and finally approve. It is used to catch bugs early, keep the code consistent, and spread knowledge, so that more than one person understands every part of the project. Pull request etiquette is the set of habits that make reviews quick and friendly, for both the author and the reviewer.
Before a school magazine goes to print, a second student proofreads every article. A good proofreader marks real problems clearly and kindly. A good writer hands over a clean draft, explains anything unusual, and doesn't take the red marks personally. Both want the same thing: a good magazine.
For the Author: Prepare the Pull Request
| Habit | Why |
|---|---|
| Keep it small | Under about 400 changed lines, a reviewer can read carefully. Split bigger work into several pull requests |
| One purpose per pull request | A bug fix and a refactor in one change hide each other |
| Review it yourself first | Read your own diff as a reviewer would, and catch the obvious problems before anyone else spends time |
| Explain the why | The description says what problem it solves, how, and how you tested it |
| Clean history | Squash "wip" and "fix typo" commits, or let the platform squash |
| Make the checks pass | Don't ask people to review a red build |
| Answer every comment | Fix it, or explain why not. Resolve the thread only when the reviewer agrees |
Self-Review with Git
Navika's branch adds the late fee. Before opening the pull request, she looks at it exactly the way a reviewer will:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
printf "class FeeCalculator {\n}\n" > FeeCalculator.java
git add .
git commit --quiet -m "Start the project"
git switch --quiet -c feature/late-fee
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
int lateFee() {
System.out.println("debug");
return 250;
}
}
EOF
git commit --quiet -am "Calculate the late fee"
git switch --quiet main
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add the README"
git switch --quiet feature/late-fee
git log --oneline main..feature/late-fee
git diff --stat main...feature/late-fee627f820 (HEAD -> feature/late-fee) Calculate the late fee
bd1eba0 Add the late fee
FeeCalculator.java | 4 ++++
fees.txt | 1 +
2 files changed, 5 insertions(+)Explanation: main..feature/late-fee lists the commits the pull request will contain. main...feature/late-fee, with three dots, compares the branch with the point where it split from main: exactly the diff a pull request shows, without the README that main got in the meantime. With two dots, git diff would also show main's new changes, reversed.
The full diff shows a problem she would rather find herself:
git diff main...feature/late-fee -- FeeCalculator.javadiff --git a/FeeCalculator.java b/FeeCalculator.java
index 5e1c1c5..1bf0e16 100644
--- a/FeeCalculator.java
+++ b/FeeCalculator.java
@@ -1,2 +1,6 @@
class FeeCalculator {
+ int lateFee() {
+ System.out.println("debug");
+ return 250;
+ }
}A leftover debugging line. She removes it before anyone else has to point it out.
For the Reviewer: Review Well
| Habit | Why |
|---|---|
| Review soon | A pull request that waits for days goes stale and blocks the author. Aim for the same or next working day |
| Understand the goal first | Read the description and linked issue before the code |
| Run it if needed | git fetch and git switch <branch> (or gh pr checkout <n>), then build and test |
| Look for what matters | Correctness, tests, security, readability, design. Leave formatting to automatic tools |
| Be kind and specific | Comment on the code, not the person. Explain why, and suggest a fix |
| Mark how important a comment is | "Blocking", "suggestion", "question" or "nit" (a small, optional detail) |
| Approve when it is good enough | Not when it is how you would have written it |
Writing Helpful Comments
| Instead of | Write |
|---|---|
| "This is wrong." | "This returns 250 even for 0 days late. Should it check daysLate > 0?" |
| "Why did you do this?" | "Question: is there a reason the fee is hard-coded here instead of using the constant?" |
| "Use a constant." | "Suggestion (not blocking): a LATE_FEE constant would make the next change easier." |
| "Fix the formatting." | (Nothing: let the formatter and CI check it) |
Many teams prefix comments with a label, such as blocking:, suggestion:, question: or nit:, so the author knows at once what must change and what is optional. GitHub and GitLab also let reviewers write a suggestion block with replacement code that the author can apply with one click.
After the Review: See What Changed
Advitha asks for a constant instead of 250. Navika also removes the debug line, cleans up with a fixup commit and autosquash, and force-pushes. To review the new version, Advitha doesn't want to read everything again, only what changed since her review. Navika keeps a name on the reviewed version first:
git branch review-1
cat > FeeCalculator.java <<'EOF'
class FeeCalculator {
static final int LATE_FEE = 250;
int lateFee() {
return LATE_FEE;
}
}
EOF
git commit --quiet --fixup=":/Calculate the late fee" -a
git rebase --quiet --autosquash main
git range-diff --creation-factor=90 main review-1 feature/late-fee1: bd1eba0 = 1: bf9dadb Add the late fee
2: 627f820 ! 2: d322e49 Calculate the late fee
@@ Commit message
## FeeCalculator.java ##
@@
class FeeCalculator {
++ static final int LATE_FEE = 250;
++
+ int lateFee() {
-+ System.out.println("debug");
-+ return 250;
++ return LATE_FEE;
+ }
}Explanation: git range-diff compares the two versions of the series, commit by commit (see "Interactive Rebase"). --creation-factor=90 lets it pair commits that changed a lot. With the default (60), it would show the old and new "Calculate the late fee" as one removed and one added commit. Two things changed: the branch is now based on the newer main (which range-diff ignores, because it compares each commit's own changes), and "Calculate the late fee" changed: the diff after ! shows that the hard-coded 250 became LATE_FEE, and the debug line is gone. GitHub and GitLab show the same comparison between pushes.
A CONTRIBUTING.md Template
Write the team's review rules down, so newcomers don't have to guess:
# Contributing to Fee Tracker
## Workflow
- GitHub flow: branch from main, open a pull request, merge when approved.
- Branch names: feature/<topic>, fix/<topic>, docs/<topic>.
## Pull requests
- One purpose per pull request, ideally under 400 changed lines.
- Describe why, what and how you tested it. Link the issue ("Closes #12").
- The build and tests must pass before review.
- At least one approval is required. Merge method: squash.
## Reviews
- Review within one working day.
- Label comments: blocking, suggestion, question or nit.
- The author resolves a thread only after the reviewer agrees.Common Mistakes
Huge pull requests. They get a quick "looks good" instead of a real review. Split them.
Arguing about style in comments. Agree on a formatter and let CI enforce it.
Approving without reading. An approval means "I understand this and I think it is correct". Say so honestly, or ask for more time.
Taking comments personally. Reviews are about the code. Answer each comment, and ask when something is unclear.
Force-pushing without telling reviewers. Comments can lose their place. Say in the pull request what changed, or push fixup commits during review and squash at the end.
Under the Hood
A pull request's diff is a three-dot diff: the platform compares the branch's tip with the merge base of the branch and its target, the commit where they split. That is why changes merged into main later don't appear in your pull request, and why git diff main...branch on your computer shows exactly the same thing. When you push new commits, the platform recomputes it. When you force-push a rebased branch, it can also compare the old and new versions, much like git range-diff.
Interview Corner
What makes a good pull request?
A single purpose, a small size, a clear description of why and how it was tested, a clean history, passing checks, and an author who has reviewed it first and responds to every comment.
What should a reviewer focus on?
Whether the change is correct and tested, secure, readable and fits the design. Formatting and style should be handled by automatic tools. Comments should be specific, kind, and marked as blocking or optional.
How do you see exactly what a pull request changes, locally?
git diff main...feature (three dots) compares the branch with its merge base on main, which matches the pull request's diff. git log main..feature lists its commits.
How do you review only what changed after the author force-pushed?
git range-diff <base> <old-version> <new-version> compares the two versions of the series commit by commit. Platforms offer the same view between pushes.
Practice
Challenge: is this pull request small enough? Measure the size of feature/late-fee compared with main: the number of files and changed lines, with git diff --shortstat, and the number of commits.
What does Git print? Before you open the solution, decide whether to use two or three dots.
Show solution
git diff --shortstat main...feature/late-fee
git rev-list --count main..feature/late-fee 2 files changed, 6 insertions(+)
2Three dots for the diff, so only the branch's own changes are counted, and two dots for the commit count (for git log and git rev-list, two dots already mean "on the branch but not on main"). Two files, a few lines and two commits: an easy review.
Checkpoint. Review a pull request in a project you know (or a public one), using labels such as blocking, suggestion and nit. Then open a pull request of your own, review it yourself first with git diff main...branch, and after the first review, use git range-diff to check your updated version.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git log --oneline main..<branch> | List a pull request's commits |
git diff main...<branch> | Show a pull request's changes |
git diff --shortstat main...<branch> | Measure its size |
git switch <branch> / gh pr checkout <n> | Check it out to run it |
git range-diff main <old> <new> | Compare two versions of a pull request |
Was this tutorial helpful?