How can I download the changes contained in a Github pull request as a unified diff?
Asked
Active
Viewed 9.3k times
355
-
Normally the PR patch link is sent to the person, who's accepting the PR. – kenorb Jun 21 '13 at 09:15
-
This isn't implemented in [GitLab](https://gitlab.com/) yet, but I created [a feature request](http://feedback.gitlab.com/forums/176466-general/suggestions/6737196-allow-a-diff-patch-file-to-be-downloaded-from-a-me) so please add your votes to it. – colan Nov 19 '14 at 16:01
-
cf. the the last ¶ of [_Pro Git_ §6.3.3: "Managing Pull Requests: Email Notifications"](https://git-scm.com/book/en/v2/GitHub-Maintaining-a-Project#_email_notifications) – Geremia Mar 06 '17 at 14:40
4 Answers
649
To view a commit as a diff/patch file, just add .diff
or .patch
to the end of the URL, for example:

Ceco
- 1,586
- 3
- 16
- 23

Simone Carletti
- 173,507
- 49
- 363
- 364
-
25Great, thanks. And there is also `.patch`. Why is this not exposed in the GUI? How is one supposed to discover this? – Thilo May 31 '11 at 14:04
-
62It's not documented to keep stackoverflow in business. Honestly, that is FAQ #2 – sehe May 31 '11 at 14:15
-
Also because `git pull` is the preferred method of downloading and applying the changes. – Tekkub Jun 01 '11 at 05:20
-
1Ooooh, thanks, this answer is worth gold. (That blogposting too.) I wonder how anyone sane can work without that, and why it is not exposed in the crappy-enough-as-is Web UI. – mirabilos Sep 01 '13 at 19:43
-
Thank you so much. This is very useful for porting changes from Github into SVN or whatever repo you have. They all accept diff and patch, so this is a great way to make changes more portable. Thanks! – Cory Trese Jan 25 '14 at 16:51
-
-
15Judging by what these return and the the links in the docs at https://developer.github.com/v3/media/#commits-commit-comparison-and-pull-requests , the `.diff` URL gives a straight diff to the default branch based on `git-diff` https://git-scm.com/docs/git-diff output, and the `.patch` URL gives a concatenation of the individual commits in the PR (each relative to their parent commit) in a format suitable for e-mailing based on `git-format-patch` https://git-scm.com/docs/git-format-patch output. – rakslice May 07 '17 at 00:10
-
1
-
2I reached here via a google search, and few hours later, I noticed that github now shows this as a "ProTip" below each PR – Mandar Vaze Nov 11 '19 at 08:11
-
-
how to use it from git clone, im use `git clone https://github.com/darkk/redsocks` , want use the patched version from https://github.com/darkk/redsocks/pull/162 – Anas Fanani Dec 09 '22 at 10:36
-
This doesn't appear to work anymore. `curl https://github.com/weppos/whois/pull/90.diff -o 90.diff` results in an empty file. – Logan Aug 22 '23 at 17:49
-
To answer my own comment.. Github will redirect you, so `curl -L https://github.com/weppos/whois/pull/90.diff -o 90.diff` will get the diff. – Logan Aug 22 '23 at 17:55
60
Somewhat related, to let git download pull request 123 and patch it into mylocalbranch
locally, run:
git checkout -b mylocalbranch
git pull origin pull/921/head

thakis
- 5,405
- 1
- 33
- 33
-
11Or to get the pull request onto a new PR branch **`git fetch origin pull/921/head:PR`** and then merge with your current branch, giving you a chance to review the changes **`git merge PR --no-commit --no-ff`** – MoonStom Mar 04 '15 at 21:08
-
5The full documentation is at https://help.github.com/articles/checking-out-pull-requests-locally/ – JBert Feb 23 '16 at 11:22
-
1This requires you to setup Git with your credentials. You cannot anonymously test a proposed change (like you could by apply a diff manually). Yet another instance of Git taking a simple workflow and making it difficult. – jww Mar 23 '17 at 18:39
6
To get the PR changes into your local repo in an staged but uncommitted state, so you can review:
git pull origin pull/123/head --no-commit
And to generate a patch file from that:
git diff --cached > pr123.diff

Bill Hollings
- 2,344
- 17
- 25
0
There is another alternative to the related solution. It answers the original question and uses git fetch
and FETCH_HEAD
.
git fetch origin pull/921/head
cat .git/FETCH_HEAD
# Then either of
git diff `git merge-base FETCH_HEAD HEAD`..FETCH_HEAD > diff.diff # Downloads the unified diff as asked in the original question
git merge FETCH_HEAD # Applies the diff

Daniel T
- 827
- 9
- 14