Skip to the content

Lesson 95 of 296

GitHub, pull requests and code review

What you will learn

  • Remotes: origin and upstream
  • clone, push, fetch and pull
  • Connecting to GitHub securely
  • Forks
  • Pull requests from start to finish
  • How to write a good pull request
  • How to review code well
  • Merge options and protected branches
On this page

Remotes

A remote is a copy of the repository on another computer, usually on GitHub. By convention the main remote is called origin.

git remote -v                          list remotes
git remote add origin git@github.com:sam/shop.git

Getting and sending changes

CommandMeaning
git clone <url>Download a full copy of a repo, including all history
git push -u origin feature/loginSend your branch to GitHub; -u links them so later you can just type git push
git pushSend new commits on the current branch
git fetchDownload new commits from the remote, without changing your files
git pullfetch then merge the remote branch into yours
git pull --rebasefetch then rebase your commits on top (Lesson 96)

fetch is always safe: it only downloads. pull changes your branch.

If push is rejected, someone else pushed first. Pull, fix any conflicts, then push again.

Connecting securely

GitHub no longer accepts your account password for Git commands. Use one of:

  • SSH keys: create a key pair on your computer (ssh-keygen -t ed25519), add the public key to GitHub, and use git@github.com:... URLs.
  • HTTPS with a token or the GitHub CLI (gh auth login), which stores credentials for you.

Never share your private key.

Forks

A fork is your own copy of someone else's repository on GitHub. Used for open source: you fork, change your copy, then open a pull request back to the original. The original repo is often added as a remote called upstream, so you can pull its latest changes.

Pull requests

A pull request (PR; GitLab calls it a merge request) asks for your branch to be merged into main. It is where code review, discussion and automated checks happen.

The flow:

  1. Create a branch from an up-to-date main.
  2. Make focused commits.
  3. Push the branch.
  4. Open a PR on GitHub with a clear description.
  5. CI (Continuous Integration) automatically runs linting, tests and a build (Part 19).
  6. Teammates review: they comment, approve or request changes.
  7. You push fixes to the same branch; the PR updates automatically.
  8. When approved and all checks pass, it is merged.
  9. Delete the branch.

Writing a good PR

Keep it small. A PR of 100 to 300 changed lines gets a careful review. A PR of 2,000 lines gets a quick "looks good to me" and hides bugs. Split big work into several PRs.

A useful description:

## What
Adds password reset by email.

## Why
Users currently have to contact support to reset passwords (ticket SHOP-142).

## How
- New POST /auth/reset-request endpoint creates a one-time token (expires in 30 minutes)
- Email sent through the existing mail queue
- New reset page validates the token and the new password

## Testing
- Unit tests for token creation and expiry
- Manually tested the full flow locally

## Screenshots
(before / after of the new page)
  • Link the ticket or issue.
  • Review your own diff first. Remove console.log, commented-out code and unrelated changes.
  • Use a draft PR to get early feedback before it is finished.

Reviewing code well

What to look for:

AreaQuestions
CorrectnessDoes it do what the ticket asks? Edge cases: empty, null, zero, very large input?
SecurityInput validated? Permissions checked on the server? Secrets kept out of the code?
TestsAre the important paths tested? Would the tests fail if the code broke?
ReadabilityClear names? Small functions? Would a new teammate understand it?
DesignDoes it fit the existing patterns? Is anything duplicated?
PerformanceUnnecessary loops, queries inside loops (the N+1 problem, Part 17)?

How to comment:

  • Be kind and specific. Comment on the code, not the person.
  • Ask questions rather than give orders: "What happens if items is empty here?"
  • Explain why, and suggest an alternative.
  • Mark minor points clearly: "Nit: could rename to totalPence." (A nit is a small, optional point.)
  • Praise good work too.
  • Respond quickly; slow reviews block teammates.

When receiving review: do not take it personally, reply to every comment, ask if something is unclear, and say thank you.

Merge options on GitHub

OptionResultGood for
Merge commitAll branch commits plus a merge commitKeeping the full detailed history
Squash and mergeAll branch commits combined into one commit on mainA clean main history: one commit per PR. Very popular.
Rebase and mergeBranch commits replayed onto main, no merge commitA linear history with individual commits kept

Protected branches

Teams protect main with rules such as:

  • No direct pushes; changes only through PRs
  • At least one approving review required
  • CI checks must pass
  • Branch must be up to date with main before merging

Other GitHub features to know

  • Issues: track bugs and tasks.
  • GitHub Actions: run CI/CD pipelines (Part 19).
  • CODEOWNERS: automatically request reviews from the right people for certain folders.
  • Dependabot: automatic PRs to update dependencies and fix security issues.

Common mistakes

  • Pushing secrets.
  • Giant PRs.
  • Empty PR descriptions.
  • Force-pushing over a teammate's work.
  • Merging your own PR without review when the team expects one.

How to say it in an interview

"I clone, create a branch from an up-to-date main, and push it to origin. fetch only downloads while pull fetches and merges, or rebases with the rebase option. Every change goes through a small, focused pull request with a description of what, why and how it was tested; CI runs lint, tests and build, and a teammate reviews it. When reviewing I check correctness, edge cases, security, tests and readability, and I phrase comments as kind, specific questions. Main is protected, and we usually squash and merge so each PR becomes one commit."

Key words

WordMeaning
RemoteA copy of the repo on another computer
originThe usual name of the main remote
upstreamThe original repo when you work on a fork
CloneDownload a full copy of a repo
Push / pull / fetchSend / download and merge / only download
ForkYour own copy of someone else's repo
Pull requestA request to merge a branch, with review
CIAutomated checks on every change
NitA small, optional review comment
Squash mergeCombining a PR's commits into one
Protected branchA branch with rules about how it can change

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.