Git Pull Request
When working in a team and collaborating on a project, we will be pushing changes to a central remote repository hosted on platforms like GitHub or Bitbucket. Pull request is a way of informing other team members about the changes that you have made. It is a way of requesting other team members to pull your changes and analyze them. Let's learn more about how pull requests work.
What are Pull Requests?
- A pull request is used to inform other team members and collaborators of the changes that you have pushed to the remote repository.
- When a pull request is created, all the developers can view and discuss your changes and can add new follow-up commits if needed. It is a convenient way of getting your code reviewed by the project manager.
- We can also push incomplete branches and start a pull request to ask for help from other developers.
- Once the changes are verified and approved by the team leader or the project manager, they can be merged with the master branch. This is why they are also known as Merge Requests.
- A pull request serves a three-fold purpose. First, it informs other developers that you have pushed changes and started a pull request. Second, it provides a platform to discuss the proposed changes, and third, it allows developers to add follow-up commits if they feel like something is missing.
How does a Pull Request work?
As discussed above, a pull request is made by a team member when he has pushed his/her code to the remote and wants to get it reviewed by others. A pull request can be made in two different scenarios. One, when the developer is a team member and working on a shared repository, and second when the developer wants to suggest changes to the authors of some other public repository. Let's discuss how to create a pull request in each of the above cases.
Pull Request for a Shared Repository
When working and collaborating on a shared repository, we will first clone the repository to our local system, and create a new branch for our work. We can then directly push changes to the remote and create a pull request to inform other developers. If our work is approved by the project manager then the branch that we created can be merged into the main branch of the repository.
Pull Request for a Forked Repository
There are probably millions of public remote repositories out there. We can suggest changes to the authors of these repositories by using pull requests. First, we will fork the public repository to our GitHub account and then clone it to our local machine. We can then create new commits or new branches with the changes that we want to suggest. Then, we can push these changes to our public forked repository. Finally, we will create a pull request to let the author of the original public repository see the proposed changes. We can discuss the changes with these authors and if they like our work, they can add it to their public repository.
Pull Requests on GitHub
GitHub is probably the most widely used Git hosting service. Let's learn how to create pull requests on GitHub.
For Shared Repository
- Suppose we are working on a team project and have a shared remote repository on GitHub with just a single master branch. First, let's clone this repository to our local system. Next, we will create a new branch called feature and add some commits to this branch. Push this new feature branch to the remote repository.
- Now, head over to the GitHub repo page and click on the "pull requests" tab.
- Click on the New Pull Request button.
Next, we need to choose the base branch and the branch that we are trying to compare. The base branch, in most cases, will be the main branch where you want to merge the changes. The compare branch will the one that we created(feature in this case).
Click on the Create Pull Request button and add a suitable description for your feature branch.
Other developers can now view and suggest changes. If everything seems fine then the project leader can merge these changes with the main branch.
For Forked Repository
First head over to the public repository to which you want to suggest changes and fork it to your GitHub account.
Clone this forked repository to your local system and make the commits for the changes that you want to include. Push the changes to your forked repository.
Now, when you will head over to your repository page on GitHub, you will be able to see a "compare and pull request" button. Click on it and add a suitable description and create a pull request.
You can also head over to the original public repository on GitHub and create a pull request from there. Make sure you click on the "compare across forks" links and choose the appropriate repositories and branches.
Pull requests are an efficient way of proposing changes to a repository. The collaborators can discuss these changes and add new commits to the branch. If everything works fine, then the new branch can be merged with the main workflow of the repository. We can also create pull requests for public repositories to suggest changes to other developers.