Pull requests
When developers work in teams, they almost never commit straight to main. Instead, they make their changes on a branch and open a pull request to ask for them to be merged. It's how code gets reviewed, discussed and improved before it becomes part of the real project.
You're working alone right now, but learning this now means you'll be ready the day you join a team or contribute to an open source project. It's also a nice way to keep your own main branch clean.
What a pull request is
A pull request, often called a PR, is a request on GitHub that says: "I made some changes on this branch. Please look at them and merge them into main."
A pull request page shows:
- What changed, line by line, in every file
- A description of why the change was made
- Comments, where others can ask questions or suggest improvements
- A merge button for when everyone is happy
The name is a little odd. You're asking the project to pull your changes in. Some sites, like GitLab, call the same thing a merge request, which might make more sense.
What is a pull request?
Select one answer
The pull request workflow
Here's the full loop, step by step:
1. Create a branch for your change and work on it:
git switch -c add-projects-section
2. Commit your changes on that branch, as usual.
3. Push the branch to GitHub. The first time you push a new branch, you have to tell Git where it should go:
git push -u origin add-projects-section
In VS Code, clicking Publish Branch does the same thing.
4. Open the pull request. Go to your repository on github.com. GitHub usually shows a yellow banner saying your branch had recent pushes, with a Compare & pull request button. Click it.
5. Describe the change. Give the pull request a clear title and write a short description: what you changed and why. Then click Create pull request.
6. Review it. Look through the Files changed tab. Reading your own changes on a different screen is surprisingly good at catching mistakes. In a team, this is where others leave comments.
7. Merge it. When it looks good, click Merge pull request and confirm. GitHub merges the branch into main and offers to delete the branch. You can safely say yes.
8. Update your computer. The merge happened on GitHub, so your local main doesn't have it yet:
git switch main git pull
Now everything is in sync again.
You merged a pull request on GitHub. Why do you need to run git pull on main afterwards?
Select one answer
Writing a good pull request
A pull request is a little message to whoever reviews it, even if that's future you. A good one has:
- A clear title, like "Add projects section to the home page"
- A short description of what changed and why
- Screenshots when the change is visual. You can paste images straight into the description box on GitHub
Keep pull requests small. One feature or one fix per pull request is much easier to review than a huge pile of unrelated changes.
Which pull request is easiest to review?
Select one answer
Make your first pull request:
- Create a branch called
add-projects-section - Add a new
sectionto your page called "Projects", with a short paragraph saying that your first project is this website - Commit and push the branch
- Open a pull request on GitHub with a clear title and a short description
- Look through the Files changed tab, then merge it
- Switch to
mainon your computer and rungit pull
Congratulations, you've just done what professional developers do many times a day.