Uncovering solutions from Github pull requests

Photo by Roman Kraft on Unsplash

Open software is amazing. There are just so many brilliant solutions out there, all result of hard collaborative work of people all over the world.

The way open software fits in the world is still evolving. It is a tool against monopolies. It has been used by big corporations to increase their power - by forking projects and building proprietary ones on top of it. And efforts are being made to find a way to pay all these amazing people that are creating solutions for free.

At any rate, Github is a treasure trove for developers. But very often projects are incomplete. They are work in progress, and it is normal to find problems. This is why almost every project in Github has the “issues” tab, precisely to point out these problems.

Another very useful tab is the “pull requests” one, which has, look at that, proposed solutions to those problems. After being verified, the maintainers may choose to merge them, and make them part of the project.

More than once I found a project that solves a problem brilliantly, but falls short of working due to a small detail. Sometimes it is because of a dependency that needs to be updated, other times the maintainers just didn’t have the time to implement that one feature you need. And the solution is right there, as a pull request.

The pull request hasn’t been merged for a reason. Maybe it has a flaw. Maybe it doesn’t follow the code standards in use by that specific project. In many cases, the solution is good, but just need a few adjustments before being merged. Meaning that it could solve your problem as it is.

So, how could you use that pull request, if it wasn’t merged yet? The PR isn’t a branch. You cannot just check it out.

Making branches out of pull requests

But there is a command that creates a new branch in your computer with the contents of the pull request:

terminal
git fetch origin pull/ID/head:BRANCHNAME

In order to do that, following Github’s instructions, do the following:

  1. Clone the project.

  2. In the page of the repository in Github, click on the pull requests tab of the repository your are interested.

  1. Find the ID number of the pull request you want. That is the number after the name of the PR.

  1. Open the terminal and go to the root of the project. Use the ID number and a new name for the branch (let’s say, “feature-fix”) in the command listed above:
terminal
git fetch origin pull/2241/head:feature-fix
  1. Switch to your new branch that is based on this pull request:
terminal
git checkout feature-fix

That’s it! There you have it, your copy with the latest features of the pull request.

You can now work on it. If you decide to make changes, you can push it back to the repository and even open a new pull request to merge it on master.

February 03, 2020.