Pull Request or PR is a very important part of working in a collaborative environment. In this article, you will learn how to create a git pull request with just a few simple steps.
If you have just joined a new company or just want to learn more about GIT and don’t know how to create a pull request, then this tutorial is for you.
Table of Contents
What is Pull Request or PR?
Let’s first understand what is Pull request or in IT terms what is a PR.
A Git pull request (PR) is a way for developers to suggest changes to a codebase. It allows a developer to make changes to a branch of code and then submit those changes to the main codebase. The PR serves as a request for the code maintainers to review the changes and merge them into the main codebase if they are deemed appropriate.
When a developer creates a PR, they are essentially asking their colleagues or other developers to review and approve their changes before they are merged into the main codebase.
This review process can help catch bugs or other issues before they make it into the main codebase. Once the changes are approved, they can be merged into the main codebase, and everyone who works on that codebase can benefit from those changes.
[Step-by-Step] How to Create a Pull Request or PR on GitHub
Creating a pull request or PR is a straightforward process in Git. If you want to create a pull request on Github.com, follow below simple steps with the help of an example and you are good to go.
Step 1: Fork the Repository
The first step in creating a pull request is to fork the repository that you want to contribute to. Forking creates a copy of the repository in your GitHub account, which you can then modify and make changes to.
Check this post if you want to check the difference between GitHub Fork and Clone.
To fork a repository, go to the repository page on GitHub, and click the “Fork” button in the top right corner. This will create a copy of the repository in your GitHub account.
Note: Forking is optional, you can also create a Pull Request or PR, by directly cloning the repository.
Step 2: Clone the Repository
Once you have forked the repository, you need to clone it to your local machine. Cloning creates a local copy of the repository on your machine, which you can modify and make changes to.
To clone the repository, go to your forked repository on GitHub and click the “Clone or download” button. This will give you the URL of the repository.
Next, open your terminal and navigate to the directory where you want to clone the repository. Then, run the following command:
git clone <repository URL>
This will clone the repository to your local machine.
Step 3: Create a Branch
Before making any changes, you should create a new branch. Creating a branch allows you to isolate your changes from the main branch, making it easier to review and merge your changes later.
To create a new branch, run the following command:
git checkout -b <branch name>
<branch name> with a name for your new branch.
Step 4: Make Changes
Now that you have created a new branch, you can make changes to the code. In this step, you can add new features, fix bugs, or make any other changes that you want to contribute.
Once you have made your changes, commit them to your branch using the following command:
git commit -am "commit message"
"commit message" with a short description of the changes you made.
Step 5: Push Changes to GitHub
After committing your changes, you need to push them to your forked repository on GitHub. To push your changes, run the following command:
git push origin <branch name>
This will push your changes to your forked repository on GitHub.
Step 6: Create a Pull Request
Now that your changes are pushed to your forked repository on GitHub, you can create a pull request. To create a pull request, go to your forked repository on GitHub and click the “New pull request” button.
GitHub will then show you the changes you have made in your branch compared to the main branch of the original repository. You can review the changes and add a title and description for your pull request.
Once you have added the title and description, click the “Create pull request” button to submit your pull request.
If you want to create a Sample or test pull request, you can use this repo and submit a PR – https://github.com/storagetutorials/PR-demo
Congratulations, you have successfully created a pull request!
Once you have created the PR, it’s time to wait for approval or feedback.
It’s possible that the maintainers of the repository will ask you to make some changes before they can merge your pull request. In this case, you will receive a notification on GitHub and can go back to your local machine to make the necessary changes.
After making the changes, commit and push them to your forked repository again. GitHub will automatically update your pull request with the new changes, and the maintainers can review them again.
Once your PR is approved, you can merge it into the main branch. In some cases, repo maintainers also merge it.
Congratulations, you have successfully contributed to an open-source project using Git and GitHub!
In summary, creating a pull request is a simple process that involves forking the repository, cloning it to your local machine, creating a new branch, making changes, committing and pushing your changes to your forked repository, and creating a pull request on GitHub.
The maintainers of the repository can then review your changes, provide feedback, and merge your changes into the main branch if they are deemed appropriate. By following these steps, you can contribute to open-source projects and collaborate with other developers around the world.
Sample Github Pull Request (PR) Template
If you are looking for a sample “Pull Request” template, please check below and modify it as per your need and situation.
## Description Describe the changes you have made in this pull request. ## Related Issue If this pull request is related to an issue, provide a link to the issue here. ## Motivation and Context Explain why you have made these changes. What is the problem that you are trying to solve? ## How Has This Been Tested? Describe the tests that you have run to ensure that your changes are working as expected. ## Screenshots If applicable, provide screenshots of the changes you have made. ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Documentation update (changes to documentation only) ## Checklist - [ ] My code follows the code style of this project. - [ ] I have performed a self-review of my own code. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have made corresponding changes to the documentation. - [ ] My changes generate no new warnings. - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] Any dependent changes have been merged and published in downstream modules.
You can customize this template to fit the specific needs of your project. By providing detailed information about your changes, you can help the maintainers of the project review and merge your pull request more efficiently.