Lesson 41 of 45 beginner

Git, Pull Requests and Code Review

Keep history readable, keep changes small, and review the code without attacking the person

Guess first

Pick an answer. You will see if you were right at the end of the lesson.

What should a good commit message mainly explain?

Open interactive version (quiz + challenge)

Real-world analogy

A team writing a shared recipe book works on a master copy in the kitchen. Each cook edits a photocopy of a page, then asks a colleague to read it before it joins the master. A small change on one page is easy to read and easy to undo. If someone rewrites the master after others have copied it, their copies no longer match, and everyone loses time. The master is main, the photocopy is a branch, and the colleague reading it is the code review.

What is it?

Git records the history of your code as commits on branches. A pull request asks to merge your branch into a shared one. It is also where teammates review the change. For a mid-level developer the habits matter as much as the commands. Keep changes small and focused, and write messages that explain why. Never rewrite history others rely on, and make reviews specific and kind. These habits make a team faster because changes are easy to read, test and undo.

Real-world relevance

A developer opens a 2,000-line pull request that mixes a rename of 40 files with a new feature. The reviewer cannot tell which changes matter, approves it after a skim, and a bug ships. Splitting it into a rename PR and a feature PR would have taken ten extra minutes. In another team a developer force-pushes a shared branch, and two colleagues lose a day of work.

Key points

Quick check

Answer from memory — scrolling up first makes it stick less.

Why is force-pushing a branch that teammates have pulled a problem?

You see a small style issue in a pull request. What is the best way to write it?

Code example

git switch main
git pull --ff-only
git switch -c feature/order-total-rounding

# Work in small steps, and say why in the message
git add src/Orders/OrderTotal.cs tests/Orders/OrderTotalTests.cs
git commit -m "Round order totals per line to match the invoice"

# Bring in what others merged, replaying my commits on top
git fetch origin
git rebase origin/main

# First push of the branch
git push -u origin feature/order-total-rounding

# After a later rebase, on my own branch only
git push --force-with-lease

Line-by-line walkthrough

  1. 1. This is a shell script, not a program. The first three lines switch to main, update it with a fast-forward only pull, and create a new feature branch.
  2. 2. git add stages exactly two files, the code and its test, instead of everything. git commit records one small step with a message that says why.
  3. 3. git fetch downloads what others merged. git rebase origin/main replays my commits on top of it, so my branch starts from the latest main.
  4. 4. The first push uses -u to create the remote branch and remember it.
  5. 5. After a later rebase the commit hashes change, so a normal push is rejected. --force-with-lease overwrites the remote branch only if it still matches what my last fetch saw. Use it only on your own branch.

Spot the bug

# develop is the shared integration branch
git switch develop
git pull
git rebase main
git push --force origin develop
Need a hint?
Who else is working on develop, and what happens to their copy?
Show answer
develop is a shared branch. Rebasing it and force-pushing rewrites history that teammates already pulled, so their copies conflict and commits can be lost. Fix: merge main into develop with a normal merge. Rebase or force-push only your own unshared branch, with --force-with-lease.

Explain like I'm 5

When many children build one big toy castle, each child builds a small part on their own table first. A friend looks at it and says what is good and what needs fixing. Then the part goes on the big castle. You never knock down the big castle while others are still building on it.

Fun fact

Linus Torvalds wrote the first version of Git in about ten days in 2005, to manage the Linux kernel. The kernel team had just lost free use of the BitKeeper tool, and needed something fast and fully distributed.

Hands-on challenge

Say it out loud in 30 seconds: "What makes a good pull request, and how do you review someone else's?" Then check: did you mention a small focused change, a clear description with tests and screenshots, reading for correctness first, separating must fix from nit, and reviewing the code and not the person?

More resources

Open interactive version (quiz + challenge) Download the whole course as PDF / Word

Lock it in

← Back to course: .NET + React Playbook