GitHub can return a pull request as raw diff text without requiring you to clone the repository. For a public pull request, append .diff or .patch to its normal URL:
https://github.com/OWNER/REPOSITORY/pull/NUMBER.diff
https://github.com/OWNER/REPOSITORY/pull/NUMBER.patch
That small URL change is useful when you need to archive a review artifact, feed a diff into another tool, inspect a pull request from the terminal, or send the change to someone who does not need the full GitHub interface.
Quick example
Starting with this pull request URL:
https://github.com/OWNER/REPOSITORY/pull/42
Use these raw URLs:
https://github.com/OWNER/REPOSITORY/pull/42.diff
https://github.com/OWNER/REPOSITORY/pull/42.patch
Open either URL in a browser to view the raw response, or download it with curl:
curl -L https://github.com/OWNER/REPOSITORY/pull/42.diff -o pr-42.diff
curl -L https://github.com/OWNER/REPOSITORY/pull/42.patch -o pr-42.patch
Keep -L so curl follows redirects. If the repository or pull request is private, use an authenticated API request instead of putting credentials in a URL.
What is the difference between .diff and .patch?
Both formats contain the code changes, but they are designed for slightly different workflows.
| Format | What it emphasizes | Useful commands | Best use |
|---|---|---|---|
.diff | Unified file changes | git apply, diff viewers | Review, analysis, or applying the combined change |
.patch | Email-style commit patches with commit metadata | git am | Preserving authorship, messages, and commit boundaries |
Choose .diff when you primarily care about the final file changes. Choose .patch when commit authorship and commit messages matter or when the recipient will apply the result with git am.
GitHub’s official pull-request API documentation exposes both a diff_url and a patch_url, and its pull-request endpoint supports the application/vnd.github.diff and application/vnd.github.patch media types. See the GitHub REST API pull-request documentation for the current response fields and media types.
Download a public pull request from the terminal
Use curl when you want a file that can be archived or passed to another command:
pr_url='https://github.com/OWNER/REPOSITORY/pull/NUMBER'
curl -L "$pr_url.diff" -o pull-request.diff
curl -L "$pr_url.patch" -o pull-request.patch
Check the response before treating it as a valid patch:
file pull-request.diff
head -n 5 pull-request.diff
A unified diff normally starts with file markers such as diff --git, ---, and +++. If the downloaded file contains HTML, an authentication page, or an error message, do not pass it directly to Git.
Download a private pull request with GitHub CLI
For a private repository, use an authenticated GitHub API request. GitHub CLI is convenient because it uses your existing gh authentication:
gh api \
-H 'Accept: application/vnd.github.diff' \
repos/OWNER/REPOSITORY/pulls/NUMBER \
> pull-request.diff
For patch format, change the media type:
gh api \
-H 'Accept: application/vnd.github.patch' \
repos/OWNER/REPOSITORY/pulls/NUMBER \
> pull-request.patch
The authenticated account must have permission to read the repository. Avoid placing a personal access token in shell history or query parameters.
Download with the REST API and curl
In automation, request the pull-request endpoint with the appropriate Accept header:
curl --fail-with-body --location \
--header 'Accept: application/vnd.github.diff' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
--header "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/OWNER/REPOSITORY/pulls/NUMBER \
--output pull-request.diff
Use a secret store for GITHUB_TOKEN, give it only the repository access it needs, and never paste a real token into a shared script or documentation example.
Review a GitHub diff in the browser
Raw diff text is compact, but it becomes difficult to navigate when a pull request changes many files. A browser diff viewer can add file navigation and side-by-side review without requiring repository access.
To review a public GitHub PR diff in SharePatch:
- Copy the pull request’s
.diffor.patchURL. - Open the SharePatch online diff and patch viewer.
- Choose URL as the input method.
- Paste the raw GitHub URL and create the review link.
SharePatch fetches the raw patch, renders its file changes, and provides side-by-side, line-by-line, raw, and download views. The resulting link is unlisted, not access-controlled, so do not import private code or secrets.
If you are reviewing a new push after a rebase, the raw PR diff may include more than the reviewer needs. Use the separate guide to remove rebase noise from a GitHub PR update.
Apply the downloaded diff safely
Always inspect a patch before applying it. In a clean working tree, check whether a .diff will apply:
git apply --check pull-request.diff
If the check succeeds, apply it:
git apply pull-request.diff
For an email-style .patch that should preserve commits and authorship:
git am pull-request.patch
If git am stops on a conflict, either resolve it and continue with git am --continue, or return to the pre-apply state with:
git am --abort
Run these commands only in a repository where you understand and trust the incoming changes. A patch can modify build scripts, CI configuration, dependencies, and other executable project files.
Common problems
The download contains HTML instead of a diff
The repository may be private, the pull request may not exist, or an authentication/abuse-protection page may have been returned. Check the HTTP status and the beginning of the file before applying it.
curl --fail-with-body -L "$pr_url.diff" -o pull-request.diff
The patch no longer matches the target branch
The pull request may have changed since the file was downloaded, or your checkout may not match the PR’s base. Fetch the current branches, confirm the intended base commit, and download a fresh artifact if necessary.
git apply works but git am does not
git apply consumes unified file changes. git am expects mailbox-style patches with commit metadata. Download the .patch representation when you need git am, or use .diff with git apply when commit metadata is not required.
Which method should you use?
- Use a
.diffURL for a simple raw view, automated analysis, orgit apply. - Use a
.patchURL when commit boundaries and authorship matter. - Use GitHub CLI or the REST API for private repositories and repeatable automation.
- Use a browser viewer when a reviewer needs easier navigation but not the entire repository.
- Use the GitHub pull request UI when the team needs inline comments, checks, approvals, and durable review history.
For other ways to move changes between people and tools, compare six practical methods for sharing a Git diff.