How can I download the changes contained in a Github pull request as a unified diff?
-
Normally the PR patch link is sent to the person, who's accepting the PR.– kenorbCommented Jun 21, 2013 at 9:15
-
This isn't implemented in GitLab yet, but I created a feature request so please add your votes to it.– colanCommented Nov 19, 2014 at 16:01
-
cf. the the last ¶ of Pro Git §6.3.3: "Managing Pull Requests: Email Notifications"– GeremiaCommented Mar 6, 2017 at 14:40
Add a comment
|
4 Answers
To view a commit as a diff/patch file, just add .diff or .patch to the end of the URL, for example:
-
26Great, thanks. And there is also
.patch. Why is this not exposed in the GUI? How is one supposed to discover this?– ThiloCommented May 31, 2011 at 14:04 -
66It's not documented to keep stackoverflow in business. Honestly, that is FAQ #2– seheCommented May 31, 2011 at 14:15
-
18Judging by what these return and the the links in the docs at developer.github.com/v3/media/… , the
.diffURL gives a straight diff to the default branch based ongit-diffgit-scm.com/docs/git-diff output, and the.patchURL gives a concatenation of the individual commits in the PR (each relative to their parent commit) in a format suitable for e-mailing based ongit-format-patchgit-scm.com/docs/git-format-patch output.– raksliceCommented May 7, 2017 at 0:10 -
2
-
2I reached here via a google search, and few hours later, I noticed that github now shows this as a "ProTip" below each PR Commented Nov 11, 2019 at 8:11
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
-
11Or to get the pull request onto a new PR branch
git fetch origin pull/921/head:PRand then merge with your current branch, giving you a chance to review the changesgit merge PR --no-commit --no-ff– MoonStomCommented Mar 4, 2015 at 21:08 -
5The full documentation is at help.github.com/articles/checking-out-pull-requests-locally– JBertCommented Feb 23, 2016 at 11:22
-
2This 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.– jwwCommented Mar 23, 2017 at 18:39
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
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