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
| Part | What it is |
|---|---|
| Base | The branch the changes go into, usually main |
| Compare (head) | The branch with the changes |
| Conversation | The description, comments and review summaries |
| Commits | Every commit on the branch |
| Files changed | The diff, where reviewers comment on single lines |
| Checks | Automatic tests and builds, once they're set up |
Opening a pull request
1.Push a branch
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-mode2.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:
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:
## 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 #12Closes #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:
| Choice | Meaning |
|---|---|
| Comment | Feedback, without approving or blocking |
| Approve | Good to merge |
| Request changes | Needs 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:
```suggestion
const theme = localStorage.getItem("theme") ?? "light";
```To try a pull request locally before approving:
gh pr checkout 42Making the requested changes
A pull request follows its branch. Commit and push to the same branch, and the pull request updates itself:
git commit -am "Handle a missing theme value"
git pushThen 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:
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
| Task | Command |
|---|---|
| Open a pull request | gh pr create |
| Open it as a draft | gh pr create --draft |
| List open pull requests | gh pr list |
| Check one out locally | gh pr checkout 42 |
| Open it in the browser | gh pr view --web |
| Merge and delete the branch | gh pr merge --squash --delete-branch |