164

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)?

sidcha
  • 649
  • 5
  • 18
umläute
  • 28,885
  • 9
  • 68
  • 122
  • 2
    possible duplicate of [Download Github pull request as unified diff](http://stackoverflow.com/questions/6188591/download-github-pull-request-as-unified-diff) – Chillar Anand Jan 01 '15 at 16:56
  • 4
    @ChillarAnand: No it's not. Your link is about a pull request and not for a diff from a single commit. – usr1234567 Aug 16 '16 at 06:32

2 Answers2

353

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...

umläute
  • 28,885
  • 9
  • 68
  • 122
  • can we directly use this url in the `git apply` command? like `git apply ` – Darshan L Nov 18 '20 at 05: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 Nov 18 '20 at 07: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 Feb 15 '21 at 15:34
  • 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 Nov 30 '21 at 19:42
  • If you have binary files in your commit or PR, use `.patch` instead of `.diff`. – Wenod Pathirana Mar 16 '22 at 07:39
  • Oh my god, the time wasted copy-pasting... – t56k Mar 24 '23 at 00:27
  • For `Content containing PDF`, it is now blocked using either .patch / .diff – Worst Jul 20 '23 at 14:35
11

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

https://github.com/foo/bar/commit/${SHA}.patch/
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jan
  • 129
  • 1
  • 2
  • 10
    as of today, I'm unable to reproduce this: https://github.com/torvalds/linux/commit/1ffaddd029c867d134a1dde39f540dcc8c52e274.patch (without trailing slash) works fine for me (tested with firefox, cURL and wget) – umläute Aug 05 '18 at 20:51