Download Github pull request as unified diff

75,268

Solution 1

To view a commit as a diff/patch file, just add .diff or .patch to the end of the URL, for example:

Solution 2

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

Solution 3

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    
Share:
75,268
hansvb
Author by

hansvb

Updated on July 08, 2022

Comments

  • hansvb
    hansvb almost 2 years

    How can I download the changes contained in a Github pull request as a unified diff?

  • hansvb
    hansvb about 13 years
    Great, thanks. And there is also .patch. Why is this not exposed in the GUI? How is one supposed to discover this?
  • sehe
    sehe about 13 years
    It's not documented to keep stackoverflow in business. Honestly, that is FAQ #2
  • Tekkub
    Tekkub about 13 years
    Also because git pull is the preferred method of downloading and applying the changes.
  • mirabilos
    mirabilos over 10 years
    Ooooh, 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.
  • Cory Trese
    Cory Trese over 10 years
    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!
  • Rohit Choudhary
    Rohit Choudhary over 9 years
    but how to ignore prefixes? a and b coming before file paths
  • MoonStom
    MoonStom about 9 years
    Or 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
  • JBert
    JBert over 8 years
  • jww
    jww about 7 years
    This 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.
  • rakslice
    rakslice about 7 years
    Judging by what these return and the the links in the docs at developer.github.com/v3/media/… , the .diff URL gives a straight diff to the default branch based on git-diff 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 git-scm.com/docs/git-format-patch output.
  • ivan_pozdeev
    ivan_pozdeev over 6 years
    @RohitKumarChoudhary use -p1 with patch.
  • Mandar Vaze
    Mandar Vaze over 4 years
    I reached here via a google search, and few hours later, I noticed that github now shows this as a "ProTip" below each PR
  • cambunctious
    cambunctious over 3 years
    Apply the PR commits to your repo in one line: curl <patch url> | git am