177

I would like to get a single commit (let's call it ${SHA}) from GitHub via the web-interface.

For example, something like:

$ git clone http://github.com/foo/bar
$ cd bar
$ git format-patch -o .. ${SHA}~1..${SHA}
$ cd ..
$ rm -rf bar

...but without having to clone the entire repository (the repo in question is large).

Obviously GitHub can display the diff of a given commit via the web interface, but how I can extract that into a (unified) diff-file (ideally, with the commit-message intact)?

2

2 Answers 2

385

OK, found the answer myself.

Adding .patch (or .diff) to the commit-URL will give a nice patch:

https://github.com/foo/bar/commit/${SHA}.patch

Thanks to Ten Things You Didn't Know Git And GitHub Could Do...

8
  • can we directly use this url in the git apply command? like git apply <url.diff>
    – Darshan L
    Commented Nov 18, 2020 at 5:12
  • 2
    @DarshanL well that is a completely different question about the powers of the git cmdline tool (and super simple to try out ;-)) and unrelated to github (github would just be a provider of such a file; but any patch file served over http:// would do) - however the short answer is probably: no
    – umläute
    Commented Nov 18, 2020 at 7:49
  • Is there also an easy way to get the patch if it is done in several commits? (instead of downloading and applying several patches)
    – baptx
    Commented Feb 15, 2021 at 15:34
  • 3
    If you get Content containing PDF or PS header bytes cannot be rendered from this domain for security reasons. with .patch, try .diff. It worked for me.
    – PF4Public
    Commented Nov 30, 2021 at 19:42
  • 1
    If you have binary files in your commit or PR, use .patch instead of .diff. Commented Mar 16, 2022 at 7:39
12

I found I had to add a / at the end, i.e.

https://github.com/foo/bar/commit/${SHA}.patch/
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.