Pull requests
Overview
A pull request (PR) is a proposal: “please merge my branch into main”. It’s the heart of collaboration on GitHub. Around that one request, GitHub gathers everything a team needs to decide whether the change is ready: the diff, a description, discussion, code review, automated test results and the merge button.
(GitLab calls the same thing a merge request.)
Opening a pull request
- Do your work on a branch and push it:
git switch -c feature/about-page
# …commits…
git push -u origin feature/about-page- Open the PR. GitHub shows a “Compare & pull request” banner for recently pushed branches, or use the Pull requests → New pull request page. From the terminal:
gh pr create --base main --title "Add about page" --body "…"
gh pr create --fill # use your commit messages as title and body
gh pr create --web # open the form in the browser- Check the base (where it will be merged — usually
main) and the compare branch (yours).
Writing a good description
Reviewers read the description first. Make their job easy:
## What
Adds an About page introducing the site's author.
## Why
Visitors asked who runs the site. Closes #12.
## How to test
1. Open about.html
2. Check the heading and introduction render correctly
## Screenshots
(before/after images for visual changes)- Title: short and specific, like a good commit subject.
- What and why: the code shows how; the description explains the purpose and any decisions.
- Links:
Closes #12links the issue and closes it automatically when the PR is merged (Issues and projects). - How to test: steps a reviewer can follow.
Templates
Add .github/pull_request_template.md to the repository and every new PR starts with its contents — a consistent structure, plus a checklist. (GitHub also accepts the file in the repository root or in docs/, and a PULL_REQUEST_TEMPLATE/ folder for several templates.)
Draft pull requests
Open a PR as a draft when the work isn’t finished but you want to share it — for early feedback, or to see the CI results:
gh pr create --draft
gh pr ready # later: mark it ready for reviewDrafts can’t be merged and don’t request reviews until they’re marked ready.
Keep pull requests small
Small PRs get reviewed faster and more thoroughly, cause fewer conflicts, and are easier to revert. Aim for one purpose per PR — a few hundred changed lines at most. If a feature is big, split it into a series of PRs that each leave main working (feature flags help hide unfinished functionality).
Updating a pull request
A PR tracks its branch, not a fixed set of commits. To respond to feedback, commit and push to the same branch — the PR updates automatically:
git commit -am "Address review: use semantic heading"
git pushIf main has moved on and the PR conflicts or is out of date:
git fetch
git rebase origin/main # then: git push --force-with-lease
# or
git merge origin/main # then: git pushGitHub’s “Update branch” button does the merge (or rebase) for you when there are no conflicts.
Checks and reviews
Before merging, a PR usually needs:
- Status checks — automated tests from GitHub Actions or other CI (chapter 10). Their results appear at the bottom of the PR.
gh pr checksshows them in the terminal. - Reviews — at least one teammate approving the change (next lesson).
Repository rules can make both mandatory (Protecting branches).
Merging
When everything is green, someone with write access merges the PR. There are three methods; the repository’s Settings → General → Pull Requests decides which buttons appear:
| Method | Result on main | Good for |
|---|---|---|
| Create a merge commit | all PR commits + a merge commit | keeping full branch history |
| Squash and merge | one new commit containing all changes | a clean history of one commit per PR |
| Rebase and merge | PR commits replayed individually, no merge commit | linear history with meaningful individual commits |
gh pr merge --squash --delete-branch
gh pr merge --auto --squash # merge automatically once checks and reviews passAfter merging, delete the branch (GitHub can do this automatically: Settings → Automatically delete head branches), and update your local main:
git switch main
git pull
git branch -d feature/about-page # may need -D after a squash mergeThat last note is worth understanding: after a squash merge, main contains a new commit, not your branch’s commits — so Git doesn’t consider your local branch merged, and -d refuses. Since the PR is merged on GitHub, -D is safe.
Pull requests between forks
You can also open a PR from your copy (fork) of someone else’s repository — the standard way to contribute to open source. Forks and contributing covers it.
A good PR is a gift to the reviewer: small, focused, described, tested, and with a clean diff. You’ll get faster, better reviews in return.
Your first pull request
In your practice repository on GitHub, create a branch feature/about-page, add an about.html page in two commits, push it, and open a pull request against main with a clear title and a description that explains what and why and how to test it. Open it as a draft first, then mark it ready for review. Add a pull request template to the repository so future PRs start with the same headings. Finally, squash-merge it and delete the branch.
# Run inside your practice repository from the previous lesson (with gh signed in)
git switch main && git pull
# 1. Branch and two commits
# 2. Push and open a draft PR with a good description
# 3. Mark it ready for review
# 4. Squash-merge and delete the branch
# 5. Add a PR template for next time (on its own branch + PR, or directly if you're alone)# Run inside your practice repository from the previous lesson (with gh signed in)
git switch main && git pull
# 1. Branch and two commits
git switch -c feature/about-page
echo "<h1>About</h1>" > about.html && git add about.html && git commit -m "Add about page"
echo "<p>I'm learning Git.</p>" >> about.html && git commit -am "Add introduction to about page"
# 2. Push and open a draft PR with a good description
git push -u origin feature/about-page
gh pr create --draft --base main --title "Add about page" --body "## What
Adds an About page introducing the site's author.
## Why
Visitors asked who runs the site.
## How to test
Open about.html in a browser and check the heading and introduction."
# 3. Mark it ready for review
gh pr ready
# 4. Squash-merge and delete the branch
gh pr merge --squash --delete-branch
git switch main && git pull
# 5. Add a PR template for next time
mkdir -p .github
cat > .github/pull_request_template.md <<'EOF'
## What
## Why
## How to test
## Checklist
- [ ] Tests added or updated
- [ ] Docs updated
EOF
git add .github && git commit -m "Add pull request template" && git pushSummary
- A pull request (PR) asks to merge one branch into another, with discussion, review and checks in one place.
- Good PRs are small and focused, with a title and description that say what changed, why, and how to test it.
- Open a draft PR to share early work; mark it ready when you want review.
- Push more commits to the same branch to update a PR; keep it current with main by rebasing or merging.
- Merge with a merge commit, a squash or a rebase — the repository settings decide which are allowed.