A pull request (PR) is how changes get into a shared project on GitHub. Instead of pushing straight to main, you push a branch and open a pull request that says: "here are my changes, please review them and merge them into main".
The pull request page shows every commit and every changed line, lets teammates comment on specific lines, runs automatic checks, and records the whole conversation. When everyone's happy, one click merges the branch.
Pull requests are the centre of teamwork on GitHub, and they're how you contribute to open-source projects you don't own. In this lesson you'll follow the complete workflow from a new branch to a merged PR.
The pull request workflow
- Update your local
main:git switch mainthengit pull. - Branch:
git switch -c add-search. - Commit your work on the branch.
- Push the branch:
git push -u origin add-search. - Open a pull request on GitHub. After a push, GitHub shows a yellow banner with a Compare & pull request button.
- Review: teammates read the changes and comment.
- Merge when approved, then delete the branch.
Writing a good pull request
- Title: what the PR does, like a commit message: "Add search box to header".
- Description: why the change is needed, what you changed, and how to test it. Add screenshots for anything visual.
- Link issues: writing
Closes #14in the description closes issue 14 automatically when the PR is merged. - Keep it small: a PR that changes 50 lines gets a careful review the same day; one that changes 2,000 lines waits.
Choose Create draft pull request if the work isn't finished but you'd like early feedback.
Review and updates
On the Files changed tab, reviewers click the + next to any line to comment on it, then submit a review that comments, approves or requests changes.
To respond to feedback, keep working on the same branch and push again. New commits appear in the PR automatically; there's no need to open a new one. Reply to each comment and mark conversations Resolved when they're addressed.
Merging and forks
The green merge button offers three options:
- Create a merge commit: keeps every commit and adds a merge commit.
- Squash and merge: combines all the PR's commits into one tidy commit on main. Many teams use this by default.
- Rebase and merge: replays the commits onto main without a merge commit.
To contribute to a project you can't push to, first click Fork to make your own copy, push your branch to your fork, then open a pull request from your fork to the original repository.
GitHub Pull Requests Syntax
Pull request commands
git switch main
git pull
git switch -c <branch>
# commit your work
git push -u origin <branch>
gh pr create --fill # or use the website button
gh pr merge --squash --delete-branchGitHub Pull Requests Summary
- A pull request proposes merging one branch into another, with review.
- Workflow: pull main, branch, commit, push the branch, open the PR, review, merge.
- Good PRs are small, clearly titled and explain why;
Closes #Nlinks issues. - Push more commits to the same branch to update a PR.
- Fork a repository to propose changes to projects you can't push to.
GitHub Pull Requests Examples
Each example shows the code, the output it produces and what each part does.
Push a branch for a pull request
git switch -c add-search
git commit -am "Add search box to header"
git push -u origin add-searchSwitched to a new branch 'add-search'
[add-search 2f4c8d1] Add search box to header
2 files changed, 41 insertions(+), 2 deletions(-)
remote: Create a pull request for 'add-search' on GitHub by visiting:
remote: https://github.com/samlee/portfolio/pull/new/add-search
To https://github.com/samlee/portfolio.git
* [new branch] add-search -> add-searchLine-by-line explanation
- Line 2-a stages changes to files Git already tracks, so add and commit happen together.
- Line 3Push the branch. GitHub even prints a link for opening the pull request.
A clear pull request description
## What
Adds a search box to the header that filters the recipe list as you type.
## Why
Visitors asked for a faster way to find recipes (see the feedback in #14).
## How to test
1. Open the home page
2. Type "soup" in the search box
3. Only soup recipes should remain
Closes #14What
Adds a search box to the header that filters the recipe list as you type.
Why
Visitors asked for a faster way to find recipes (see the feedback in #14).
How to test
1. Open the home page
2. Type "soup" in the search box
3. Only soup recipes should remain
Closes #14 (issue 14 will close automatically when this is merged)Line-by-line explanation
- Lines 1 to 2Say what changed in a sentence.
- Lines 4 to 5Explain why, linking the discussion.
- Lines 7 to 10Give reviewers exact steps to try it.
- Line 12Links and closes the issue on merge.
Open and merge from the terminal
gh pr create --title "Add search box to header" --body "Closes #14"
gh pr merge 15 --squash --delete-branchhttps://github.com/samlee/portfolio/pull/15
✓ Squashed and merged pull request #15 (Add search box to header)
✓ Deleted branch add-searchLine-by-line explanation
- Line 1GitHub CLI opens the PR without visiting the website.
- Line 2After approval, squash-merge it and delete the branch in one step.
Common GitHub Pull Requests Mistakes
Working directly on main
git switch main
git commit -m "Add search"
git pushgit switch -c add-search
git commit -m "Add search"
git push -u origin add-search
# then open a pull requestOn most teams, main is protected and only changes through reviewed pull requests.
Opening a new PR for every fix
PR #15 Add search box
PR #16 Fix search box typo
PR #17 Fix search box againPR #15 Add search box
(push the fixes to the same branch; they appear in #15)A PR with no description
Title: changes
Description: (empty)Title: Add search box to header
Description: what, why, how to test, Closes #14GitHub Pull Requests in Real Projects
Pull requests at work
At most software companies, every change to the product goes through a pull request: automated tests run on it, at least one teammate approves it, and only then can it be merged. Open-source projects such as React receive thousands of pull requests from volunteers around the world in exactly this way.
GitHub Pull Requests FAQ
How do I change the base branch of a pull request?
Click Edit next to the pull request's title, and a menu for the base branch appears. Choose the branch you meant to merge into. The list of commits and files updates to compare your branch against the new base.
How do I check out someone else's pull request locally?
With the GitHub CLI, run gh pr checkout 42. With plain Git, run git fetch origin pull/42/head:pr-42 and then git switch pr-42. Both work for pull requests from forks too, so you can run and test the code before approving it.
Can I reopen a closed pull request?
Yes, as long as it wasn't merged and its branch still exists. Use the Reopen pull request button at the bottom of the conversation. If the branch was deleted, restore it first or open a new pull request.
Why does my pull request show commits I didn't make?
Usually your branch started from another feature branch instead of main, so it carries that branch's commits too. It can also happen when the base branch is wrong. Check the base branch, and if needed rebase your commits onto main with git rebase --onto main old-base my-branch.
Should I delete the branch after a pull request is merged?
Yes. Its commits are now in main, and deleting the branch keeps the branch list short and clear. GitHub shows a Delete branch button after the merge, and a repository setting can delete head branches automatically. Locally, run git branch -d and git fetch --prune.
GitHub Pull Requests Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the command that pushes a new branch for a pull request.
git push add-footer
What is a pull request?
A reviewer asks for a change on your open PR. What do you do?
What happens when a PR whose description contains Closes #8 is merged?
How do you update a pull request after review feedback?
GitHub Pull Requests Quiz
5 questions to check what you learned.
How do you contribute to a repository you can't push to?
- Fork it, push a branch to your fork, then open a PR
- You can't
- Ask GitHub support
- Clone it and push to main
What does "Squash and merge" do?
- Deletes the PR's commits
- Merges without review
- Combines all the PR's commits into a single commit on the target branch
- Merges several PRs at once
Where do reviewers leave comments on specific lines?
- In the README
- In the commit message
- On the Files changed tab of the pull request
- In an issue
Why are small pull requests better?
- GitHub limits PR size
- They're quicker and easier to review well, and easier to undo
- They merge faster technically
- They don't need descriptions
What does "Squash and merge" do?
- Deletes the pull request
- Merges without review
- Rebases main onto the branch
- Combines all the PR's commits into one commit on the target branch