Skip to content

How to apply unmerged upstream pull requests from other forks into my fork?

An answer to this question on Stack Overflow.

Question

A project on GitHub that I have a fork of has a new pull requests that I want to pull into my fork that the author has not pulled in yet.

Is there a simple way to apply pull request from other forks into my fork? Is there something else here that I am missing?

Answer

I use a handy dandy script for this. I run the script by typing:

git prfetch upstream

and it gets all of the pull requests from the upstream fork.

To create the script make a file ~/bin/git-prfetch.

The file should contain the following:

#!/bin/bash
if [ -z "$1" ]; then
    echo "Please supply the name of a remote to get pull requests from."
    exit 1
fi
git fetch $1 +refs/heads/\*:refs/remotes/$1/\* +refs/pull/\*/head:refs/remotes/$1/pr/\*

Ensure that your path includes the script by setting:

export PATH="$HOME/bin:$PATH"

You can add this file to ~/.bashrc to make the change permanent.

Now, make sure you add the fork you want to get the pull requests from:

git remote add upstream https://github.com/user/repo.git

And then

git prfetch upstream