493
votes

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?

7
There is an instruction: gist.github.com/piscisaureus/3342247Piotr Migdal
Does this reflect on the network graph at GitHub if you use git commands to do it?ADTC
Might also be interesting to find all the branches too: stackoverflow.com/q/47798937/10245Tim Abell

7 Answers

316
votes

Update: Via Webpage

You can also do this via the github webpage.

I assume, you should have already a fork (MyFork) of the common repo (BaseRepo) which has the pending pull request from a fork (OtherFork) you are interested in.

  1. Navigate to the fork (OtherFork) which has initiated the pull request which you like to get into your fork (MyFork)
  2. Go to the pull requests page of OtherFork
  3. Click new pull request
  4. The pending pull request(s) should be offered. Remember to select proper OtherFork branch too. Select on the left side as the base fork your fork (MyFork) (IMPORTANT).
  5. Now the option of View pull request should change to Create pull request. Click this.

Now you should have a pending pull request in your fork (MyFork), which you can simply accept.

289
votes

You can do it manually quite easily:

  • add the other fork as a remote of your repo:

    git remote add otherfork git://github.com/request-author/project.git
    
  • fetch his repo's commits

    git fetch otherfork
    
  • You have then two options to apply the pull request (if you don't want to choose pick 1.)

    1. If you don't care about applying also the eventual commits that have been added between the origin and the pull request, you can just rebase the branch on which the pull request was formed

      git rebase master otherfork/pullrequest-branch
      
    2. If you only want the commits in the pull request, identify their SHA1 and do

      git cherry-pick <first-SHA1> <second-SHA1> <etc.>
      
78
votes

Like Tekkub said previously, you can just pull the branch in directly. Most of the time with GitHub, the branch is simply "master" on the requesting User's fork of the project.

Example: git pull https://github.com/USER/PROJECT/ BRANCH

And as a pratical example:

Say you forked a github project called safaribooks and there is the following pull request, in the originating project, that you want to put in your fork:

enter image description here

Then, in your fork's cloned project folder, run:

git pull https://github.com/fermionic/safaribooks.git fix-str-decode
27
votes

Pull requests for the project may come from many different authors (forks), and you probably don't want a separate remote for each fork. Also, you don't want to make any assumptions about the branch the author used when submitting the pull request, or what else might be in the author's master branch. So it's better to reference the pull request as it appears in the upstream repository, rather than as it appears in the other forks.

Step 1:

git remote add upstream <url>

You've probably already done this step, but if not, you'll want a remote defined for the upstream project. The URL is the clone URL of the project you forked. More info at Configuring a remote for a fork and Syncing a fork. upstream is the name you are giving to the remote, and while it can be anything, upstream is the conventional name.

Step 2:

git pull upstream refs/pull/{id}/head

... where {id} is the pull request number. upstream is the name of the remote to pull from, i.e. just "upstream" if you followed step 1 exactly. It can also be a URL, in which case you can skip step 1.

Step 3:

Type in a commit message for the merge commit. You can keep the default, although I recommend giving a nice one-line summary with the pull request number, the issue it fixes, and a short description:

Merge PR#42, fixing VIM-652, support for mapping arbitrary IDEA actions
22
votes

Some more detailed info that worked for me.

My .git/config file for the forked repo looks like this:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = false
[remote "origin"]
        url = [email protected]:litzinger/angular-carousel.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
        rebase = true
[remote "source"]
        url = git://github.com/revolunet/angular-carousel.git
        fetch = +refs/heads/*:refs/remotes/source/*
        fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Then run "git fetch source", which then listed all the pull requests from the forked repo.

 * [new ref]         refs/pull/54/head -> origin/pr/54
 * [new ref]         refs/pull/67/head -> origin/pr/67
 * [new ref]         refs/pull/69/head -> origin/pr/69
 * [new ref]         refs/pull/71/head -> origin/pr/71

And then to merge in a specific pull request run "git merge master origin/pr/67"

9
votes

What I would do is the following;

git checkout master
git remote add #NAME# #ADDRESS TO REPO#
git fetch #USERNAME#
git checkout -b test_fork
git rebase #NAME#/#BRANCH#

I have now merged the changes into a test branch, named test_fork. So that any changes won't dirty my tree.

Optionally you can use cherry-pick as described above to pick a particular commit if that is more preferable.

Happy travels :)

1
votes

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