読み物Git · Collaboration

Level: Intermediate

Pull Requests, Merge Requests, Code Reviews, and Team Collaboration

Propose changes that are easy to review, review them well, choose a merge method, protect the main branch, and collaborate through forks or email patches.

Edeeson Opina8 min read

An Intermediate lesson card: lesson 7 of the Git & Version Control series, Pull Requests, Merge Requests, Code Reviews, and Team Collaboration, with the Miso fox sitting.

What you’ll learn

  • Follow the lifecycle of a pull or merge request
  • Write small, well-described changes
  • Review for design and correctness, with clear comments
  • Choose between merge, squash, and rebase merges
  • Protect branches and use code owners
On this page
  1. Before you start
  2. What a pull request is
  3. The lifecycle of a change
  4. Writing a change that's easy to review
  5. Keep it small
  6. Explain the why
  7. Review your own change first
  8. Reviewing well
  9. Reviewing locally
  10. Merge methods
  11. Rules that protect the main branch
  12. Contributing from forks
  13. Review by email
  14. Common mistakes and troubleshooting
  15. Practical exercise: a full review cycle
  16. Key takeaways
  17. References
  18. What's next

On most teams, code doesn't go straight onto the main branch. It's proposed as a change, discussed, reviewed, checked by automation, and only then merged. GitHub, Bitbucket, Azure Repos, Gitea, and Forgejo call this a pull request; GitLab calls it a merge request; projects that work by email send a patch series. This lesson covers the lifecycle of a proposed change, how to write one that's easy to review, how to review well, the merge methods platforms offer, and how the same ideas work with forks and email.

Before you start

You should be comfortable with branches and remotes from the Basic level, and know the platform landscape from GitHub vs GitLab vs Bitbucket vs Codeberg: Understanding Git Hosting Platforms. Having two accounts, or a teammate, on any platform helps for the exercise.

What a pull request is

A pull request (PR) or merge request (MR) is not a Git feature. It's a platform object that says: "please merge branch X into branch Y," with a discussion attached. Underneath, it's ordinary branches and commits; the platform adds:

  • a diff view of the proposed change;
  • comments, on the whole change or on specific lines;
  • reviews: approve, comment, or request changes;
  • status checks from CI (lesson 10 and 11) showing whether tests pass;
  • rules such as required approvals before merging;
  • a merge button that performs the merge with the method the project allows.

Because it's platform data, the conversation stays on the platform when you move a repository. Lesson 15 covers exporting it.

The lifecycle of a change

  1. Branch from the current main branch: git switch -c fix/cart-rounding.
  2. Commit focused changes with clear messages.
  3. Push the branch: git push -u origin fix/cart-rounding.
  4. Open the PR or MR. Platforms usually print a link after the push. Open it as a draft if it's not ready, to share early or get CI feedback.
  5. Automation runs: tests, linters, and builds report status on the change.
  6. Review: reviewers comment; you answer and push more commits to the same branch, and the PR updates automatically.
  7. Approve and merge once checks pass and the rules are met.
  8. Clean up: delete the branch, and pull the updated main branch locally.

Writing a change that's easy to review

The author does most of the work that makes review effective.

Keep it small

Review quality drops sharply as changes grow. A few hundred changed lines can be reviewed carefully; a few thousand get skimmed. Split large work into a sequence of PRs, each one useful and safe on its own: first the refactor that makes room, then the feature, then the cleanup.

Explain the why

The diff shows *what* changed. The description must say *why*, and anything the reviewer needs to verify it:

MARKDOWN
## Why
Checkout showed "-0.00" when every item in the cart was free (#412).

## What changed
- Round totals in every case, not only when the total is positive.
- Add tests for an all-free cart and a single free item.

## How to verify
1. Add only the free sample to the cart.
2. Open checkout: the total shows 0.00.

## Notes for reviewers
The rounding helper moved to `money.ts`; the rest is unchanged.

Many platforms support a template file (for example, .github/pull_request_template.md on GitHub, or description templates in .gitlab/merge_request_templates/ on GitLab) so every PR starts with these headings.

Review your own change first

Before asking anyone, read your own diff on the platform as a reviewer would. You'll catch debugging leftovers, unrelated formatting changes, and missing tests, and save a review round.

Reviewing well

Good review finds problems and spreads knowledge, without becoming a gatekeeping ritual. Look at, roughly in order of importance:

  1. Design: does this change belong here, and does it fit the system? Is there a simpler approach?
  2. Correctness: does it do what it claims, including edge cases and errors? Are there tests, and do they test the right thing (see How to Write Effective Test Cases)?
  3. Security and data: input handling, permissions, secrets, personal data.
  4. Readability: will the next person understand it? Are names clear?
  5. Style: only what automated tools don't already enforce. Let formatters and linters handle formatting.

How you comment matters as much as what you find:

  • Be specific and kind. Comment on the code, not the person.
  • Say how important each comment is. Prefixes such as "nit:" (minor), "question:", or "blocking:" prevent misunderstandings about what must change.
  • Explain why, and suggest an alternative when you can. Many platforms let you propose an exact replacement that the author can apply with one click.
  • Approve when the change makes the code better overall, even if it's not exactly how you'd have written it.
  • Respond quickly. Slow reviews push authors toward bigger, riskier PRs.

Reviewing locally

Some changes are easier to judge by running them. Fetch the branch and try it:

Shell
git fetch origin fix/cart-rounding
git switch fix/cart-rounding
npm test

For PRs from forks, platforms expose read-only references you can fetch directly. GitHub publishes them as refs/pull/<number>/head and GitLab as refs/merge-requests/<number>/head:

Shell
git fetch origin refs/pull/412/head:review/412
git switch review/412

Merge methods

Platforms usually offer three ways to merge, and projects often restrict them to one:

  • Merge commit: keeps every commit from the branch, plus a merge commit. Full history, but a busier graph.
  • Squash merge: combines all the branch's commits into one commit on the main branch. Clean main history, one commit per change; the individual commits are lost from the main branch (the platform usually keeps them in the PR).
  • Rebase merge (or fast-forward): replays the branch's commits onto main, with no merge commit. Linear history that keeps each commit, which works best when every commit is clean and passes tests.

Squash merging is popular because it forgives messy work-in-progress commits. If you use it, make the final squashed message good: it's the one that stays in history.

Rules that protect the main branch

Platforms let you protect important branches. Common rules:

  • no direct pushes; changes arrive only through reviewed PRs;
  • at least one approval, and new pushes dismiss earlier approvals;
  • required status checks (CI must pass) before merging;
  • branches must be up to date with main before merging, or a merge queue tests the combined result;
  • no force pushes or deletion.

A code owners file (CODEOWNERS, supported by GitHub and GitLab, among others) automatically requests reviews from the people responsible for particular paths:

CODEOWNERSText
# Payments code needs a payments reviewer
/src/payments/   @your-org/payments-team
# Workflows and deployment need an ops reviewer
/.github/        @your-org/ops

Contributing from forks

On open-source projects, contributors usually can't push branches to the original repository. Instead they fork it (make their own copy on the platform), push a branch to their fork, and open a PR from the fork to the original. Keep your fork's main branch in sync by fetching from upstream, as in lesson 5, and start each new branch from the updated main.

Maintainers can often push small fixes to a contributor's branch if the contributor allows it, which saves a review round for trivial changes.

Review by email

Projects such as the Linux kernel and Git itself review changes as emailed patches, and platforms like SourceHut are built around this. Git has the tools built in:

Shell
git format-patch -3 --cover-letter -o outgoing/   # the last three commits as patch files
git send-email --to=list@project.test outgoing/*.patch
# A maintainer applies them with:
git am outgoing/*.patch

Reviewers reply inline, and a revised version of the series is sent as "v2". The ideas are the same as with pull requests: small, well-explained changes, line-level feedback, and iteration.

Common mistakes and troubleshooting

  • One giant PR. Split it; reviewers can't hold thousands of lines in their heads.
  • An empty description. Say why, what, and how to verify.
  • Mixing a refactor with a behavior change. Reviewers can't tell which lines matter. Separate them.
  • Arguing style in review. Automate it with a formatter and linter.
  • Approving without reading or running anything. Review is a responsibility; if you didn't check, say what you didn't check.
  • "This branch has conflicts." Update your branch from main (merge or rebase), resolve locally, and push.
  • CI passes locally but fails on the PR. The PR may be tested against the latest main; fetch and rebuild with it.

Practical exercise: a full review cycle

With a teammate, or two accounts:

  1. Protect main on a test repository: require one approval and a passing check, and disallow direct pushes.
  2. As author, create a branch with a small change and a deliberate small mistake. Push and open a PR with a why, what, and how-to-verify description.
  3. As reviewer, fetch the branch locally, run it, and leave one blocking comment and one "nit:" comment, including a suggested change.
  4. As author, apply the suggestion, fix the mistake, push, and reply.
  5. Approve, merge with squash, and check the final commit message on main.
  6. Optional: send the same change as a patch with git format-patch and apply it in a fresh clone with git am.

Key takeaways

  • Pull requests and merge requests are platform features built on ordinary branches and commits.
  • Small, focused changes with a clear description get better reviews, faster.
  • Review design and correctness first; automate style.
  • Merge commits, squash merges, and rebase merges trade history detail against readability.
  • Protect main with required reviews and checks; use code owners for important paths.
  • Forks and email patches follow the same principles.

References

What's next

How long should branches live, and how do they flow into releases? Git Branching Strategies: Trunk-Based Development, Git Flow, and Feature Branches compares the main approaches.

See every lesson in Git & Version Control: From Fundamentals to Advanced Workflows