24

I'm using the NSDate-Extensions plugin in my iOS Xcode project. The master repo has some errors which seems to be fixed in two Pull Requests:

These requests are still open and not accepted to the master repo. How can I add these to my local repo in a Git-way? It would be fine, if my local repo gets update, if their Fork gets updated later:

dhrm
  • 14,335
  • 34
  • 117
  • 183
  • possible duplicate of [Pull requests from other forks into my fork](http://stackoverflow.com/questions/6022302/pull-requests-from-other-forks-into-my-fork) – CharlesB Feb 25 '13 at 13:35
  • possible duplicate of [git: how can i fetch an unmerged pull request for a branch i don't own?](http://stackoverflow.com/questions/6743514/git-how-can-i-fetch-an-unmerged-pull-request-for-a-branch-i-dont-own) – Ciro Santilli OurBigBook.com Jun 02 '14 at 14:35

4 Answers4

21

You can pull the requests from the command line without adding remotes:

git pull https://github.com/erica/NSDate-Extensions +refs/pull/6/head

If you are not already sure you want to do that, then you might want to check out that branch first with:

git fetch https://github.com/erica/NSDate-Extensions +refs/pull/6/head:refs/origin/pull/6

The advantage of this compared to using the patch files is that git gets the actual commits. So git can see when master was previously merged into the pull request. The patch file is completely unaware of such differences.

JonnyJD
  • 2,593
  • 1
  • 28
  • 44
15

Add a .patch to the end of the pull url - you can download and apply the patch on your repo:

curl https://github.com/erica/NSDate-Extensions/pull/6.patch | git am
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • After I've done the above command, the patch applied successfully, but when I'm running a `git status` it says that there isn't any changes. Why can this patch change my local repo and still say that there isn't any changes? – dhrm Jan 17 '12 at 14:15
  • @manojlds: nice trick! your answer also applies to http://stackoverflow.com/questions/6022302/pull-requests-from-other-forks-into-my-fork – CharlesB Feb 25 '13 at 13:39
  • 1
    @DennisMadsen: `git am` doesn't *not* just apply the patch. pull/*.patch is actually multiple patches (one per commit in the pull request) And each patch is applied as commit. So after that there are no uncommited changes (which git status could display). – JonnyJD Aug 07 '13 at 06:35
  • This only works with simple pull request that never merged commits from master. For that reason I would always prefer using the actual pull request branches as in http://stackoverflow.com/a/18096344/1904815. – JonnyJD Aug 07 '13 at 06:44
  • Looks like something has changed at GitHub. That url is just a redirect/link to `https://patch-diff.githubusercontent.com/raw/erica/NSDate-Extensions/pull/6.patch`. Substituting the new url works, though. – Sparhawk Aug 19 '15 at 23:19
  • @Sparhack @dhrm You can use `curl -L` to follow redirects if you prefer the old URL format. – Soren Bjornstad Oct 11 '20 at 13:50
12

You should add the forks that offered the pull requests, and add them as a remote:

  • Find the person contributing the link (normally just click the sha1 hash)

  • Do something like:

    git remote add githubuser theirgithubfork.git

Then you can easily pull down their changes:

  • git fetch githubuser

Easily take individual commits with"

  • git cherry-pick thesha1fromthepullrequest

For a complete example, imagine this pull request (6bbbcc5) from RogerE on the Ruby Capistrano project.

$ git clone git@github.com:capistrano/capistrano.git
$ cd capistrano
$ git remote add RogerE https://github.com/RogerE/capistrano.git
$ git fetch RogerE
$ git cherry-pick 6bbbcc5
Lee Hambley
  • 6,270
  • 5
  • 49
  • 81
  • So I should clone the original repo from erica, and run "git remote add" for both the forks by Ricardo1980 and exalted? What if I afterwards make a "git pull", would it just pull from the original repo -- or would updates from the two remotes gets pulled too? – dhrm Jan 17 '12 at 08:11
  • Why are you both making a fetch and afterwards a cherry-pick? Wouldn't the fetch pull down all from the remote? – dhrm Jan 17 '12 at 08:15
  • `git pull` would pull from the original repo. You could specify which repo to pull from by specifying the remote name and the refspec. e.g. `git pull RogerE master`. – Ramon Marco L. Navarro Jan 17 '12 at 08:22
  • `git fetch` would just download the changes but won't actually merge it yet until you specify a command to merge them e.g. `git merge` or `git rebase`. – Ramon Marco L. Navarro Jan 17 '12 at 08:25
  • Ok, so there is no way using a single pull command to get updates from the three repos at once? – dhrm Jan 17 '12 at 08:25
  • No, you have to add each repository you want to take the changes from as a remote, `fetch` them all (download commits). And then `cherry-pick` the commits that you want. Cherry-picking preserves the authorship of the commits, and the `SHA1` hash. If the `origin` repository ever does pull them into master, there's no problems with merge conflicts if you work this way. – Lee Hambley Jan 17 '12 at 11:08
  • When you make `git pull` that actually (by default) expands to `git pull origin master`. In your example your `origin` remote is `erica`. So when you make `git pull` you pull the master branch from `erica`. The remotes are not involved at all. This is by design. – Lee Hambley Jan 17 '12 at 11:09
-3

You can pull specific pull requests like this:

git pull https://github.com/erica/NSDate-Extensions/pull/6
git pull https://github.com/erica/NSDate-Extensions/pull/7

imo this is much easier than adding a remote (especially when it is just one-off fixes) or piping via curl.

MichielB
  • 4,181
  • 1
  • 30
  • 39
  • The idea is correct, but it doesn't work like that, which is possibly the reason for the downvote. I added a working answer with the same idea at http://stackoverflow.com/a/18096344/1904815) – JonnyJD Aug 07 '13 at 07:03