Tutorial: Making your first Pull Request. How to do it?

Contributing to open source project is a great way to enhance your skills. Not only you learn how to write quality code that actually goes into production you also learn how to work collaboratively with other developers.
There are many platforms where open source projects are being hosted by many open source organizations. Most famous of them all is GitHub. Other famous platforms are Gitlab and Bitbucket. In this blog post I will describe the correct way to open a pull request so that you don’t run into several issues.

What is Pull Request?

Pull request in simple terms a patch of code that is sent to original code base for review. Maintainers of the project will review your pull request and merge it into original code base if everything is perfect.

Is it tough to open a pull request?

No, not at all. It is quite simple to start. It just takes you to come with correct code changes, do a code commit and then send it along to original project for review.

Then why this blog post?

I came across many of my colleagues who face difficulty when they started with open source and wanted to open a pull request. Most of them were confused with how to create branch, what is the correct way, how to make changes later on and the list goes on. So I thought writing this blog post in case some may fin it helpful.

Making a Pull Request

I will take the help of my own code repository for this. The repository is hosted on GitHub. You can use the repository along side for experimenting following this tutorial.
Making a pull request requires you to have a fork of the original project. You cannot make changes to original code repository so you should fork the repository. After you fork the repository clone your forked repository to your system using git clone <YOUR-FORKED-REPO-URL>.git. Then go to the repository using cd <Repo-Name>.
Execute git remote -v You will see a github url that is pointing to your forked repository and has been named origin. Now add one more remote URL named upstream which will point to origin repository. Execute git remote add upstream <Original-Project-Repository-URL>.git . Note that the remote URL pointed by upstream to original code repository will be used for updating your master branch with the latest code changes in main repository while we will push our code changes to our fork and in a separate branch.
Flow to create a pull request
The diagram above explains the basic flow followed for making a pull request. Now before you make any pull request make sure your master branch is updated from upstream (main code base). Execute git pull upstream master. Now suppose that you have decided to make a change in the code and create a pull request. So start by creating a new branch git branch <branch-name> and enter that branch by git checkout <branch-name>.
Make the necessary code changes and then commit the changes by executing following commands. Stage your files for commit git add . and commit by git commit -m "Commit message". Now its time to push the changes to your fork and make the pull request. Execute git push origin <branch-name> and this will create a new branch on github that will contain the commit with your code changes. Now go to that branch and you will get an option to open a pull request.
Selecting a branch and creating a pull request

What Next?

While you are waiting your pull request to get merged you can go back to master branch and create a new branch from there and try adding a new feature or fixing a new bug. Once your PR is merged you can delete the branch and update your fork master branch by git pull upstream master This will make sure that your master branch has new changes. This helps by making sure your code does not has conflicts when you create a new PR as it has new changes.

Points to remember

  1. Never make pull request from main branch, mostly name as master.
  2. Try out every new feature and bug fixing in new branch so that you don’t end up messing master branch code.

About me

My name is Pradeep Gangwar, an Information Technology undergraduate student at Indian Institute of Information Technology, Allahabad.
I love to contribute to open source projects. I love developing things with Python and JavaScript.
In case you want to know more about me you can follow me on GitHub, LinkedIn and Twitter. :D

Comments

Popular posts from this blog

SPOJ- PIE solution

Developing a Google Data Studio Connector: Part 2

Developing a Google Data Studio Connector : Part 1