Diff for commit with GitHub API

Here is how to get the diff of a commit using the GitHub API.

For a project I need to programmatically get the diff of a commit of a pull request, and as every good programmer would do, I first checked this topic on Stack Overflow because for sure someone else ran into this problem before and asked about it. That might be the case here, but it must be very well buried somewhere. But I found this post showing how to get that diff using curl instead of GitHubs REST API, which works well on a command line.

The GitHub API is not very clear about it. It just says “You can pass the appropriate media type to fetch diff and patch formats.” – but thats already the right hint we need. Now we can tweak the example we found a little, and here is how:

https://api.github.com/repos/{owner}/{repo-name}/commits/{ref}

The header of this GET request should contain { 'accept' : 'application/vnd.github.v3.diff' }

Now this request will return to us with the real diff file as content.

Leave a Reply