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.
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@v4with:fetch-depth:0 # history, tags, and git describe all need this- run:|git rev-parse --is-shallow-repository # expect falsegit 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.