How to Create a Pull Request in GitHub: Step-by-Step Guide
To create a pull request in GitHub, first push your changes to a new branch in your repository. Then, go to the GitHub website, navigate to your repository, and click
Compare & pull request to open a pull request for review and merging.Syntax
A pull request in GitHub is created through the web interface after pushing your changes to a branch. The key steps are:
git push origin your-branch: Upload your branch with changes to GitHub.- On GitHub, click
Compare & pull requestto start the pull request. - Fill in the title and description to explain your changes.
- Submit the pull request for review and merging.
bash
git checkout -b your-branch # Make changes to files git add . git commit -m "Describe your changes" git push origin your-branch
Example
This example shows how to create a pull request after making a change in a new branch.
bash
git checkout -b feature-update # Edit README.md or other files git add README.md git commit -m "Update README with new info" git push origin feature-update # Then on GitHub website: # 1. Go to your repo # 2. Click 'Compare & pull request' button # 3. Add title and description # 4. Click 'Create pull request'
Output
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Resolving deltas: 100% (0/0), done.
To github.com:username/repository.git
* [new branch] feature-update -> feature-update
Common Pitfalls
Common mistakes when creating pull requests include:
- Not pushing your branch to GitHub before trying to create a pull request.
- Creating pull requests from the default branch instead of a feature branch.
- Not providing a clear title or description, making it hard for reviewers to understand changes.
- Forgetting to sync your branch with the main branch, causing merge conflicts.
bash
Wrong: git push origin main # Trying to create pull request from main branch Right: git checkout -b feature-branch # Make changes # Commit and push # Then create pull request from 'feature-branch'
Quick Reference
| Step | Command or Action | Description |
|---|---|---|
| 1 | git checkout -b your-branch | Create and switch to a new branch |
| 2 | git add . | Stage your changes |
| 3 | git commit -m "message" | Commit your changes with a message |
| 4 | git push origin your-branch | Push branch to GitHub |
| 5 | Click 'Compare & pull request' on GitHub | Open pull request for review |
| 6 | Fill title and description | Explain your changes |
| 7 | Click 'Create pull request' | Submit for review and merging |
Key Takeaways
Always create a new branch for your changes before pushing.
Push your branch to GitHub before creating a pull request.
Use the GitHub web interface to open and submit the pull request.
Provide clear titles and descriptions for easier review.
Keep your branch updated with the main branch to avoid conflicts.