# gh pr diff: View a Pull Request Diff in CI

> gh pr diff prints the diff of a pull request, optionally name-only or in patch format. Reference for --name-only, --color, --patch, and CI auth errors.

Source: https://latchkey.dev/learn/command-reference/gh-pr-diff  
Updated: 2026-06-30

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

| Flag | What it does |
| --- | --- |
| --name-only | List only the names of changed files |
| --patch | Output in git patch format |
| --color <when> | Colorize: always, never, or auto |
| -w, --web | Open 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
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### 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.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
