How To Create a Pull Request on GitHub

What is pull request?

Pull request let you tell others about changes you’ve pushed to a github repository. Once a pull request is sent, the maintainers of that project can review the changes and discuss for modifications, and even push commits if necessary and once everything is ok they can accept that pull request and all your changes will be merged with the upstream project. Below there are set of instructions that will describe how to create pull request.

Photo by Brina Blum on Unsplash

Fork the Repository

The first step should be creating a copy of a repository. You can fork a repository on GitHub by navigating with your browser to the GitHub URL of the open-source project you would like to contribute to.

GitHub repository URL contains both the username of the owner of the repository and the name of the repository. For example, below github url aniruddha2000 is the username and the hello-world is the name of the repository.

https://github.com/aniruddha2000/hello-world

When you go to that url on your browser you will see a “Fork” button on your upper right hand side of the page.

Click the fork button and start the forking process.

Once the process is done, your browser will go to a screen similar to the repository image above, except that at the top you will see your username before the repository name, and in the URL you will see your username before the repository name.

https://github.com/your-username/hello-world

Clone the forked repository

Now you have to clone the repository in your local machine that you have forked. In order to do that you have to use git clone command. Execute below command in your terminal.

$ git clone https://github.com/your-username/hello-world.git

Create a New Branch

Whenever you work on a collaborative project you and other programmers contribute to the repository will have different ideas for new features or fixes. So it is recommended that you should always work on a new branch and make pull request to the upstream project from that branch and this process will help you to work nicely will all contributors. Execute below command to create a new branch.

$ git checkout -b new-branch
Switched to branch 'new-branch'

Make changes locally

Once you have modified all necessary files you should add them git add and commit by git commit -m. After committing you have to push the changes in the ‘new-branch’. Execute below command for this operation.

$ git push origin new-branch

Create Pull Request

Now you can create a pull request to the upstream project. You can navigate to your forked repository and click the “New pull request”.

Once you have chosen, for example, the master branch of the original repository on the left-hand side, and the new-branch of your forked repository of the right-hand side, you should see a screen that looks like this:

Add the necessary comment and then click the “Create pull request” button. Now go the pull request section of the upstream repository and you will see your pull request is there.

Thank you 🙂

Advertisement

Filter your commits through git log

The purpose of any version control system is to record changes to your code. This gives you the power to go back into your project history and see who contributed what and when and figure out where bugs were introduced. But, having all of this history available is useless if you don’t know how to navigate it. That’s where the git log command comes in. Below various git log options that will be very helpful while maintaining a big project:

Photo by Brina Blum on Unsplash

Oneline

The --oneline option will give you a output of commits to a single line. By default it will show you the commit ID and first line of the commit message.

aniruddha ~/Desktop/foobar master 
$ git log --oneline 
505a2c6 (HEAD -> master) Add the file hellomars.txt and update helloworld.txt
fb8c32d First commit

By amount

The most basic filter that git gives us the limit the number of commits. You can pass -<n> after the git log.

$ git log -3

By author

When you are looking for commits created by a specific person then --author="name" will be the option you have to use. This will return all commits matches with the author name you have given.

$ git log --author="Aniruddha"

You can use regular expressions to search for commits. In the below example the following commands searches for commits by either Aniruddha or Sayan.

$ git log --author="Aniruddha\|Sayan"

By commit message

To filter the commits by their commit message, use the --grep flag. This works like the --author flag discussed in the above but it matches against the commit message instead of the author.

$ git log --grep="First"

By file

Some times you are only interested in changes that happened to a particular file. To see the history related to a file you just have to to pass the file path. Below we want to see the commits related to foo.py.

$ git log -- foo.py

See changed file names

If you want to see the files you have changed according to your git commit then execute the below git command.

$ git log --name-only

Thank you 🙂