Skip to content
LatchkeyLatchkey home

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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

gh pr diff: View a Pull Request Diff in CI?
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.
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.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card