GitHub Pull Requests: What Opening One Lets You Do
In this article
Opening a GitHub pull request lets you propose changes to a repository and request that the maintainers review, discuss, and merge them into the main codebase. It is the primary way contributors collaborate on GitHub, providing a structured space for code review, automated checks, and conversation around a specific set of changes. In short, a pull request is a formal request to merge your branch into another branch, with tools to make that review process efficient and transparent.
Background: Why Pull Requests Exist
Git itself is a distributed version control system, meaning every developer has a full copy of the repository. Before pull requests, teams often shared patches via email or used ad-hoc methods to integrate changes. GitHub introduced pull requests in 2008 as a way to streamline collaboration, especially for open-source projects where contributors might not have write access to the main repository.
The core idea is simple: you make changes on a separate branch, push that branch to GitHub, and then open a pull request asking the repository owner to merge it. This creates a dedicated interface where reviewers can see the exact differences, leave comments on specific lines, and run automated tests before the code is integrated. It turns what could be a chaotic process into a controlled, reviewable workflow.
How a Pull Request Works
When you open a pull request, you are essentially saying: "Here are my changes on this branch, please compare it to the base branch (often main) and consider merging." GitHub then shows a comparison view, listing every file that changed, with additions highlighted in green and deletions in red. This diff is the heart of the review process.
The pull request also has its own conversation thread, where reviewers and the author can discuss the changes. You can mention specific lines in the diff, ask questions, or request modifications. As you push new commits to the branch, the pull request automatically updates, so reviewers always see the latest state. Once everyone is satisfied, a maintainer can merge the pull request, which integrates your changes into the base branch.
What You Can Do With a Pull Request
Opening a pull request unlocks several capabilities beyond just proposing code:
- Request code review: You can explicitly ask specific people or teams to review your changes. Reviewers can approve, request changes, or leave comments.
- Run automated checks: Many repositories use continuous integration (CI) tools like GitHub Actions. These run tests, linting, and other checks automatically on the pull request, showing pass/fail status directly on the page.
- Discuss the implementation: The conversation tab lets you explain your reasoning, ask for help, or debate design decisions. This is often where the most valuable collaboration happens.
- Track related issues: You can reference issues in the description (e.g., "Fixes #42") so that merging the pull request automatically closes the linked issue.
- Collaborate on the branch: If you allow edits from maintainers, they can push commits to your branch, helping you fix problems without you having to do it yourself.
- Preview the changes: For repositories with a deployed preview environment, a pull request can generate a temporary URL to see the changes live.
How to Choose: Which Branch to Target
One common question is whether to target the default branch (usually main or master) or a development branch. It depends on the project's workflow. Many projects use a branching model where main is always deployable, and all changes go through a develop branch first. Always check the repository's contributing guidelines or README for instructions.
If you are contributing to an open-source project, you typically fork the repository first, then create a branch in your fork. When you open a pull request, you set the base repository to the original project and the base branch to the one you want to merge into (often main). The head repository is your fork, and the compare branch is your feature branch.
Common Problems and How to Avoid Them
Several issues frequently trip up new contributors:
- Merge conflicts: If the base branch has changed since you branched off, your changes may conflict. Git will show you which files have conflicts, and you can resolve them either on GitHub or locally by merging the base branch into your branch.
- Accidentally committing secrets: Never commit API keys, passwords, or credentials. If you do, they are in the repository history even if you delete them later. Use a tool like
git-secretsor a pre-commit hook to catch these. - Too many commits: Some maintainers prefer a clean history with one commit per logical change. You can squash your commits before merging, either via the "Squash and merge" option on GitHub or by rebasing locally.
- Not updating your branch: If your branch is behind the base branch, you may see a notification in the pull request. Clicking "Update branch" will merge the latest base branch into yours, often resolving conflicts automatically.
Pull Requests vs. Direct Pushes
In many projects, contributors do not have write access to the main repository, so a pull request is the only way to get changes in. But even when you do have write access, pull requests are often preferred because they provide a review trail and catch issues before they reach the main branch. Direct pushes to main are risky, especially on shared projects, because they bypass review and can break the build for everyone.
Pull requests also allow for asynchronous collaboration. Reviewers can look at the changes at their own pace, and the conversation is preserved for future reference. This is much more transparent than a direct push, where the only record is the commit message.
Safety and Best Practices
Pull requests are generally safe because they do not affect the base branch until someone explicitly merges them. However, there are a few things to keep in mind:
- Review your own diff: Before opening a pull request, look at the files changed to make sure you did not accidentally include unrelated files or debugging code.
- Write a clear description: Explain what your changes do, why you made them, and how to test them. This helps reviewers understand your intent.
- Keep pull requests small: A focused pull request with a single purpose is easier to review and less likely to introduce bugs. If you are fixing multiple issues, split them into separate pull requests.
- Be responsive: When reviewers leave comments, address them promptly. If you cannot, say so and ask for more time.
Frequently Asked Questions
What is the difference between a pull request and a merge request?
They are essentially the same thing. GitLab uses the term "merge request," while GitHub uses "pull request." Both refer to the same workflow: propose changes, review, and merge.
Can I open a pull request without forking?
Yes, if you have write access to the repository, you can create a branch directly in the repository and open a pull request from there. If you do not have write access, you must fork the repository first.
What does "draft" pull request mean?
A draft pull request is one that is not ready for review. It is useful for early feedback or to show work in progress. Draft pull requests cannot be merged until they are marked as "Ready for review."
How do I update a pull request after I've opened it?
Simply push new commits to the branch that the pull request is based on. The pull request will automatically update to include the new commits. You can also use the "Update branch" button to merge the latest base branch into your branch.
What happens if I close a pull request?
Closing a pull request does not delete the branch or the commits. It simply removes the pull request from the open list, and the changes are not merged. You can reopen it later if needed.
Can I see who has reviewed my pull request?
Yes, the pull request page shows a list of reviewers and their status (approved, requested changes, or no review). You can also see the review comments in the conversation tab.