Download Github pull request as unified diff
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Downloading a GitHub pull request as a unified diff is useful when you want to review changes offline, archive a patch, or apply the change to another clone without using the GitHub UI. A pull request is still just a change set between two Git states, and GitHub exposes that change set directly in diff-friendly formats.
What a Unified Diff Actually Is
A unified diff is a text representation of file changes. It shows which files changed, where each edit happened, and which lines were added or removed.
Important line prefixes:
- '
---and+++identify the old and new file paths.' - '
@@identifies the changed hunk location.' - '
-marks removed lines.' - '
+marks added lines.'
Example:
This format is portable and works well with git apply, code review tools, and CI scripts.
Download the Pull Request Diff from GitHub
If a pull request URL looks like this:
https://github.com/OWNER/REPO/pull/123
You can download the unified diff by appending .diff:
GitHub also exposes a patch view:
In practice:
- Use
.diffwhen you want file-level changes. - Use
.patchwhen you want commit-oriented patch data, often forgit am.
Inspect the Diff Before Applying It
A downloaded diff is just a text file, so inspect it like any other patch.
If you want only the changed file list:
You should verify:
- The patch targets the repository you expect.
- The changed files match the pull request description.
- The diff is still relevant to your current local branch state.
This check matters because a pull request diff can become stale relative to the latest base branch.
Apply the Diff to a Local Repository
If you already have the target repository cloned, validate first:
If the check succeeds, apply it:
--check is important because it tells you whether the patch still matches local files without modifying anything.
If you are working from the patch variant and want commit metadata preserved, use:
Use git am only when the patch is structured as commits you want to replay.
Alternative: Fetch the Pull Request as a Branch
Sometimes a downloaded diff is not the best tool. If you have repository access, fetching the pull request ref gives you a real branch to inspect and test.
This is better when:
- You need full commit history.
- You want to rebase or merge normally.
- You want to run tests against the exact pull request branch.
Use the unified diff when portability matters more than full Git history.
Use Diffs in Automation
Unified diffs are handy in security scanning or review automation because they can be downloaded without checking out a contributor branch.
Example script:
This keeps review workflows simple in isolated build environments.
Diff Versus Patch in Practice
Both formats represent the same change set, but they are optimized for slightly different uses.
- '
.diffis ideal for inspection andgit apply.' - '
.patchis ideal when email-style commit information matters.'
Choosing the right one avoids awkward tooling mismatches later.
Common Pitfalls
- Downloading the pull request HTML page instead of the
.diffor.patchendpoint. - Applying the diff against the wrong branch state.
- Using
git amwhen you only need raw file changes. - Skipping
git apply --checkand discovering conflicts after modification. - Assuming a downloaded diff proves the pull request still merges cleanly with the latest base branch.
Summary
- GitHub pull requests can be downloaded directly as
.diffor.patch. - Unified diffs are portable text files that show file changes with context.
- '
git apply --checkis the safest way to validate before applying locally.' - Use
.patchwithgit amwhen commit metadata matters. - Fetch the pull request branch instead when you need full Git workflow rather than a portable patch file.
Related reading
- Download old version of package with NuGet
- Download single files from GitHub
- Download single files from GitHub
- ECS/ECR is common practice to have one repository per image and associated versions?
- Edit changeset comment after updates have been checked in to TFS
- Edit the root commit in Git?
- Elastic Beanstalk Ruby/Rails need to install git so bundle install works.. but is not
- Empty Git submodule folder when repo cloned
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.