Applying Pull Requests from another fork using git

When I wrote my post about using full-text search with Mastodon last week, I wrote that "you can use the standard Git tools to apply the PR".

Since then I've been asked repeatedly for more details, so I figured I'd make this a short post.

The basic question is this: I have a fork of an open source project. Someone else has another fork of the same project, and has made some modifications on a branch. They may have filed a Pull Request that isn't being merged quickly enough, or maybe they haven't (yet), but I want to incorporate these changes. So, how can I merge a branch from another fork onto my own fork?

This is actually quite straightforward, and I'll run through two options to do this here. As an example, I'll be applying VyrCossont's 'Extended Search' Pull Request for Mastodon to my own fork of Mastodon.

Applying a Pull Request from another fork using GitHub

If both your own branch, and the one you want to merge, are on GitHub, you can use GitHub to create a Pull Request and merge that. This is probably the easiest way of merging a branch from another fork, as you get a nice GUI that will guide you through it, and show you the changes before you apply them.

Here is how:

Firstly, open your own fork of the project. Open the Pull Requests tab, and click 'New Pull Request':

Creating a Pull Request

Then choose the other fork's repository as 'Head repository', and the branch you want as 'compare'. Select your own repository and branch as 'base':

Selecting branches for Pull Requests

You will see a live preview of any commits included in this merge as you make your selection. Click 'Create pull request' and you are ready to review and merge it.

The advantage of merging the branch through GitHub, is that you get a nice UI with diffs and everything, so if you aren't used to the command line this is probably the best way of doing this, if you are able to.

Applying a Pull Request from another fork manually

Alternatively you can apply this Pull Request using the command line. It's actually really straightforward too, but obviously you will be losing the GUI if you do this, which - particularly for larger, less straightforward merges - might be a problem.

Obviously, the same can almost certainly be done in any git GUI client as well.

Firstly, you'll need to add the other fork's repository as a remote to your own:

git remote add vyrcossont

(vyrcossont here is an arbitrary name that you'll use to reference this remote locally - you can choose whatever you want.)

Secondly, you'll want to fetch a copy of that repository:

git fetch vyrcossont

Finally, merge the branch for the PR you are after:

git merge vyrcossont/search-all-visible-toots

Here vyrcossont is the name you've chosen for the remote above, and search-all-visible-toots is the name of the branch you are after.

And that's it - you have now applied the changes from the Pull Request locally.