Skip to content
Latchkey

gh pr diff: View a Pull Request Diff in CI

gh pr diff shows the changes in a pull request, the same diff GitHub renders in the Files tab.

For lint-only-changed-files or diff-size gates, gh pr diff gives the exact change set without juggling base and head SHAs by hand.

What it does

gh pr diff fetches the diff between a pull request and its base. With no argument it uses the PR for the current branch. --name-only lists just the changed paths, useful for scoping a linter or test run.

Common usage

Terminal
gh pr diff 123
gh pr diff 123 --name-only
gh pr diff --color always | less -R
gh pr diff 123 --patch > pr.patch

Flags

FlagWhat it does
--name-onlyList only the names of changed files
--patchOutput in git patch format
--color <when>Colorize: always, never, or auto
-w, --webOpen the diff in a browser
-R, --repo <owner/repo>Target a specific repository

In CI

Set GH_TOKEN: ${{ github.token }} and permissions: { pull-requests: read, contents: read }. Use --name-only to feed a list of changed files into a path filter, and pass the PR number explicitly since detached-HEAD checkouts have no branch to infer from.

Common errors in CI

"gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable" means GH_TOKEN is missing. "could not determine base repository" with no number means the checkout has no remote PR context; pass the number and -R. A truncated diff usually means the PR exceeds the API diff size limit, in which case fetch the patch via git instead.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →