Git and GitHub · Part 3 · GitHub and remote repositories

Pull requests

Why not just merge?

A branch could be merged into main locally and pushed. On a team, that means code reaches main without anyone else seeing it. A pull request (PR) is a request to merge one branch into another, with a page around it: the changes, a discussion, reviews, and automatic checks. Nothing is merged until someone clicks the button.

GitLab calls the same thing a merge request.

The parts of a pull request

PartWhat it is
BaseThe branch the changes go into, usually main
Compare (head)The branch with the changes
ConversationThe description, comments and review summaries
CommitsEvery commit on the branch
Files changedThe diff, where reviewers comment on single lines
ChecksAutomatic tests and builds, once they're set up

Opening a pull request

1.Push a branch

Terminal
git switch main
git pull
git switch -c feature/dark-mode
# work and commit
git commit -am "Add dark mode toggle"
git push -u origin feature/dark-mode

2.Create it on GitHub

GitHub shows a yellow Compare & pull request banner on the repository page. Click it (or go to the Pull requests tab, then New pull request), check that base is main and compare is the feature branch, then write a title and description.

Or from the terminal:

Terminal
gh pr create --title "Add dark mode toggle" --body "Closes #12"

3.Write a description

The reviewer reads the description before any code. A short, common shape:

Pull request description
## What
Adds a dark mode toggle to the header. The choice is saved in localStorage.

## Why
Users asked for it in #12.

## How to test
1. Run npm run dev
2. Click the moon icon in the header
3. Reload: the theme should stay

Closes #12

Closes #12 links issue 12, and closes it automatically when the pull request is merged into the default branch. Fixes and Resolves work the same.

4.Ask for a review

In the right-hand sidebar, pick Reviewers. A pull request that isn't ready yet can be opened as a draft (the arrow next to Create pull request), to show work early without asking for a review.

Reviewing a pull request

In Files changed, hover over a line and click + to comment on it. Comments collect into one review, and Review changes ends it with one of three choices:

ChoiceMeaning
CommentFeedback, without approving or blocking
ApproveGood to merge
Request changesNeeds fixing before it's merged

A suggestion proposes an exact fix that the author can apply with one click. In a line comment, click the suggestion icon, or type:

Review comment
```suggestion
const theme = localStorage.getItem("theme") ?? "light";
```

To try a pull request locally before approving:

Terminal
gh pr checkout 42

Making the requested changes

A pull request follows its branch. Commit and push to the same branch, and the pull request updates itself:

Terminal
git commit -am "Handle a missing theme value"
git push

Then reply to each comment, or click Resolve conversation, and click Re-request review next to the reviewer's name. To bring in new commits from main, rebase and force-push with --force-with-lease (Lesson 18), or click Update branch on the pull request.

Merging

Once approved and the checks pass, click Merge pull request. The arrow next to it offers three ways to merge: a merge commit, squash and merge, or rebase and merge. Then click Delete branch, and locally:

Terminal
git switch main
git pull
git branch -D feature/dark-mode

-D because after a squash or rebase merge, the branch's own commits aren't in main, so -d refuses.

What makes a good pull request

  • Small. Under about 400 changed lines gets a real review; 2,000 gets a quick “looks good”.
  • One thing. A bug fix and a refactor are two pull requests.
  • Reviewed by the author first. Reading the Files changed tab before asking catches leftover logs and typos.
  • Explained. What, why and how to test, with a screenshot for visual changes.

Quick reference

TaskCommand
Open a pull requestgh pr create
Open it as a draftgh pr create --draft
List open pull requestsgh pr list
Check one out locallygh pr checkout 42
Open it in the browsergh pr view --web
Merge and delete the branchgh pr merge --squash --delete-branch