47

I downloaded a trunk version of a codebase from git, and there are build errors. Aparently a patch is now available, and I received an email :

see https://github.com/JustinTulloss/zeromq.node/pull/47 for patch

I am new to git so I'm not quite sure what to do with this 'patch' especially, since the page looks more like a discussion thread.

Does anyone know how I can obtain/apply this patch to my locally cloned git repository?

Koraktor
  • 41,357
  • 10
  • 69
  • 99
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341

5 Answers5

83

Save the patch somewhere. If you're using linux you can use curl:

curl -L https://github.com/JustinTulloss/zeromq.node/pull/47.patch > /tmp/47.patch

To apply the patch use git apply. You can see if the patch will apply cleanly with the check option. Change to your git directory and run:

git apply --check /tmp/47.patch

If it looks like you want to apply the patch remove the check option

git apply /tmp/47.patch
Abe Voelker
  • 30,124
  • 14
  • 81
  • 98
Andrew
  • 7,286
  • 3
  • 28
  • 38
  • 1
    thanks very much for the instructions. I have done exatctly as you said. It seems the patch the wrong one though. FML – Homunculus Reticulli Oct 19 '11 at 19:46
  • What do you do if the repository is private? In this case, I'm getting an (expected, but not helpful) 404 when I `curl https://github.com/username/private_repo/pull/42.patch`. I'm resorting to opening up the patch from my browser and saving it (the horror!) – kindrobot Aug 20 '14 at 20:32
  • 3
    To put the commit (including author name, commit comment, etc) in your history, use `git am` rather than `git apply`. – Alex D Aug 13 '15 at 17:02
  • @Josh, you really puzzled me until I realized you were answering apocryphalauthor, not me. – Alex D Oct 07 '15 at 19:02
  • @apocryphalauthor , if the repo is private, you can download the patch from within the browser. Just navigate to your pull request and append .patch to the end of the url. That will load the patch in your browser. Then just hit ctrl-s to save it. – Josh Oct 08 '15 at 16:08
  • 2
    FYI the `.patch` URL concatenates all the individual commits in the PR branch (relative to whatever the previous commit was at the time) and so may not apply cleanly to the current master. You can use `.diff` to get a diff to the current master. – rakslice May 07 '17 at 00:02
19

Just add a .patch at the end to get the patch:

https://github.com/JustinTulloss/zeromq.node/pull/47.patch

You can do something like below:

$ git checkout master
$ curl http://github.com/JustinTulloss/zeromq.node/pull/47.patch | git am
$ git push origin master

http://help.github.com/send-pull-requests/

manojlds
  • 290,304
  • 63
  • 469
  • 417
8

The rule seems recently changed.

Previously we took a PR and add a .patch at the end to get the patch

http://github.com/[group]/[project]/pull/30583.patch

But now the link is redirect(301) to

https://patch-diff.githubusercontent.com/raw/[group]/[project]/pull/30583.patch

So if you use curl, you could pipe with git apply command to apply a git patch from the Pull Request

curl https://patch-diff.githubusercontent.com/raw/[group]/[project]/pull/30583.patch | git apply

If the patch is not right for you now, use git apply -R command to rollback the change.

gasolin
  • 2,196
  • 1
  • 18
  • 20
5

To let git download pull request 47 and patch it into mylocalbranch locally, run:

git checkout -b mylocalbranch
git pull origin pull/47/head

If the pull request isn't on the origin repo, run

git remote add patchremote https://github.com/JustinTulloss/zeromq.node
git pull patchremote pull/47/head
thakis
  • 5,405
  • 1
  • 33
  • 33
3
git fetch -q origin +refs/pull/47/merge:
git checkout -qf FETCH_HEAD
Halton Huo
  • 31
  • 2
  • Here you are only fetching and checking out the PR. The OP additionally needs to apply (merge) the PR into his own branch. I'm upvoting anyway, as the fetch command line is a very handy. – Alberto Sep 29 '16 at 14:03