GitHub Pull Requests
A pull request (PR) is a request to merge your branch into another branch on GitHub. It is how teams review and discuss code before it reaches main.
The Team Workflow
Most teams follow this simple flow:
- Pull the latest
main. - Create a branch for your task.
- Commit your work on that branch.
- Push the branch to GitHub.
- Open a pull request.
- A teammate reviews it and suggests changes.
- When approved, the PR is merged into
main.
main always stays working, and every change is checked by at least one other person.
Step by Step
Example
git switch main
git pull
git switch -c add-contact-pageMake your changes and commit:
Example
git add contact.html
git commit -m "Add contact page with map and phone number"
git push -u origin add-contact-pageremote: Create a pull request for 'add-contact-page' on GitHub by visiting: remote: https://github.com/ayesha-dev/my-site/pull/new/add-contact-page To https://github.com/ayesha-dev/my-site.git * [new branch] add-contact-page -> add-contact-page
Open the link GitHub prints, or click Compare & pull request on the repository page.
Writing a Good Pull Request
- Title: what the PR does, like "Add contact page"
- Description: why the change is needed and how to test it
- Keep it small: a PR with 100 changed lines gets a better review than one with 3,000
- Link the issue it fixes by writing "Closes #12" in the description
Reviewing a Pull Request
On the PR page, the Files changed tab shows the diff. Reviewers can:
| Action | Meaning |
|---|---|
| Comment | Ask a question or suggest something on a line |
| Approve | The change looks good to merge |
| Request changes | Something must be fixed first |
To fix requested changes, just commit again on the same branch and push. The PR updates automatically:
Example
git commit -am "Fix phone number format on contact page"
git pushBe kind in reviews. Comment on the code, not the person, and explain why you suggest a change.
Merging the PR
When approved, click Merge pull request on GitHub. GitHub offers three ways:
| Option | Result |
|---|---|
| Create a merge commit | Keeps all commits plus a merge commit |
| Squash and merge | Combines all PR commits into one clean commit |
| Rebase and merge | Puts the commits on top of main without a merge commit |
Then update your local copy and clean up:
Example
git switch main
git pull
git branch -d add-contact-pageDeleted branch add-contact-page (was 6e0a2f9).
Forks: Contributing to Other Projects
You cannot push to someone else's repository. To contribute to an open-source project:
- Click Fork on GitHub to make your own copy of the repository.
- Clone your fork, create a branch, commit and push to your fork.
- Open a pull request from your fork to the original repository.
The project's maintainers then review and merge it. Contributing to open source is a great way to build experience and your GitHub profile.
Issues
GitHub Issues are a to-do list for the project: bug reports, feature ideas and tasks. Each has a number like #12, and a PR that says "Closes #12" closes the issue automatically when merged.
Exercise
Exercise
What is a pull request on GitHub?
