Sub topic 2
What a pull request is
This chapter assumes you can commit, branch, push, and pull with Git, and that you have a GitHub repository you can push to. It covers what a pull request is and what it holds. The next chapter covers opening one.
A request to merge, with a discussion attached
A pull request (PR) is a GitHub object that proposes merging one branch into another. Git has no such concept. Git only knows branches and commits. GitHub adds the pull request as a layer on top, so people can discuss and check a change before it lands.
Every pull request has two branches:
- The head branch holds your changes. GitHub also calls it the compare branch.
- The base branch is where you want the changes to go, usually
main.
The pull request does not copy any code. It points at the two branches and shows the difference between them. If you push another commit to the head branch, the pull request updates by itself.
What a pull request contains
A pull request bundles four things:
| Part | What it shows |
|---|---|
| Commits | The commits on the head branch that are not on the base branch |
| Files changed | The combined diff between the two branches |
| Conversation | The description, comments, review verdicts, and events such as pushes and label changes |
| Checks | Results from automated jobs such as tests and linters |
The conversation is a timeline of everything that happens to the pull request. It stays attached to the change after the merge, so it works as a record of why the code looks the way it does.
How the diff is calculated
The diff on a pull request compares the head branch against the point where it split from the base branch, which is the merge base. It does not compare against the current tip of the base. Changes that landed on main after you branched do not show up as your deletions.
You can reproduce this locally with the three-dot form of git diff:
git fetch origin
git diff origin/main...HEAD
git log --oneline origin/main..HEADThe first command shows the changed files and lines. The second lists the commits. Together they match the Files changed and Commits tabs.
Pull request states
A pull request is always in one of these states:
- Open. The change is under discussion and can still receive commits.
- Draft. It is open but marked as not ready for review.
- Merged. The head branch's changes are now in the base branch.
- Closed. It was dismissed without merging. You can reopen it.
Common mistakes
- Treating the pull request as a snapshot. It follows the head branch, so later pushes appear in it.
- Choosing the wrong base branch. A pull request aimed at a release branch instead of
mainshows a very different diff. - Mixing unrelated changes in one head branch. Reviewers must read all of them together, which slows the review.
Exercise
You branched feature from main. Afterward, two other commits landed on main. Which changes appear in the pull request from feature into main, and why?
Show answer
Only the commits on feature made since it split from main. GitHub diffs the head branch against the merge base, not against the current tip of main, so the two newer commits on main are not shown as removed.