How to Fetch and Checkout Specific Pull Requests Locally Using Git Commands

You can fetch GitHub Pull Requests locally using git fetch origin refs/pull/[PR-Number]/head for single reviews, or configure a refspec to map all PRs to remote-tracking branches under refs/remotes/origin/pr/* for persistent access.

Working with Pull Requests (PRs) locally allows you to run tests, perform diffs, and resolve merge conflicts before integrating changes. According to the tiimgreen/github-cheat-sheet repository, GitHub exposes PRs as special references under refs/pull/, which are not fetched by default during a standard git fetch. The repository's README.md documents several methods to retrieve these references and check them out as if they were ordinary branches.

Understanding GitHub Pull Request References

When you create a Pull Request on GitHub, the platform stores the PR's head commit in a special reference namespace: refs/pull/[number]/head. These references exist on the remote repository but are excluded from the default fetch refspec (+refs/heads/*:refs/remotes/origin/*). To access them locally, you must explicitly tell Git to map these remote references to your local repository using refspecs.

Method 1: Fetch a Single PR for Temporary Review

For a quick inspection or one-time merge, fetch a specific PR directly into FETCH_HEAD. This approach does not create a local branch or permanent reference.

git fetch origin refs/pull/123/head

After fetching, FETCH_HEAD points to the PR's commit. You can compare it against your current branch:

git diff FETCH_HEAD

Or merge it immediately:

git merge FETCH_HEAD

This method appears in the Fetching a specific PR section of the README.md (line 665) and is ideal for maintainers who need to verify a fix without cluttering their local branch list.

Method 2: Fetch All PRs as Remote-Tracking Branches

To persistently access all Pull Requests as named references, add a wildcard refspec that maps every PR to a remote-tracking branch under origin/pr/*.

Run the following command to fetch all current and future PRs:

git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'

Once fetched, list available PRs:

git branch -r | grep origin/pr

Checkout PR #42 directly:

git checkout pr/42

This approach is documented in the Acquiring all PR branches section (line 670) of the repository.

Permanent Configuration via git config

To make this refspec persistent for the current repository, add it to your remote configuration:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"

Manual Configuration in .git/config

Alternatively, edit .git/config directly to include the additional fetch line:

[remote "origin"]
    url = git@github.com:tiimgreen/github-cheat-sheet.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

This configuration example appears at line 774 in the README.md, demonstrating how to maintain multiple fetch refspecs for a single remote.

Method 3: Global Configuration for All Repositories

If you frequently review PRs across multiple repositories, configure the refspec globally. This adds the PR fetch rule to your ~/.gitconfig, applying it to every repository you clone that has an origin remote.

git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"

As noted in the Global config section (line 998), this ensures git fetch automatically retrieves all PRs for any repository using the standard origin remote name.

Checking Out Pull Request Branches

Once you have fetched the PR references, you can check them out using several strategies depending on your workflow needs.

Temporary Checkout from FETCH_HEAD

When using the single PR fetch method, FETCH_HEAD contains the commit SHA. Check it out temporarily:

git checkout FETCH_HEAD

Note that this puts you in a detached HEAD state. Any commits made here will be lost if you switch branches without creating a new branch.

Working with Remote-Tracking Branches

If you configured the persistent refspec, PRs appear as remote-tracking branches (e.g., origin/pr/42). Checkout using the short reference name:

git checkout pr/42

This command, shown at line 808 in the README.md, works because Git automatically resolves pr/42 to refs/remotes/origin/pr/42 when unique.

Creating Local Branches from PRs

To modify a PR or perform additional work, create a local branch that tracks the PR reference:

git checkout -b my-feature pr/42

This creates a new local branch my-feature based on the PR's current head, allowing you to commit changes, rebase, or merge without affecting the original PR reference.

Summary

  • GitHub PRs are stored under refs/pull/[number]/head and excluded from default fetches.
  • Single PR fetching uses git fetch origin refs/pull/123/head for quick, temporary access via FETCH_HEAD.
  • Persistent access requires adding +refs/pull/*/head:refs/remotes/origin/pr/* to your remote fetch configuration in .git/config.
  • Global setup applies the PR refspec to all repositories via git config --global.
  • Checkout methods include temporary FETCH_HEAD inspection, direct checkout of pr/[number], or creating local branches for active development.

Frequently Asked Questions

Where are GitHub Pull Requests stored in Git?

GitHub stores Pull Requests as references in the refs/pull/ namespace on the remote server. Each PR has at least a head reference pointing to the tip of the branch being merged, and often a merge reference showing the result of merging into the base branch. These are not standard branches under refs/heads/, so standard git fetch commands do not retrieve them without explicit refspec configuration.

Can I fetch PRs from forks using these commands?

These commands fetch PRs from the remote you specify (typically origin). If the PR exists in a fork, you must add that fork as a separate remote (e.g., git remote add fork https://github.com/forkuser/repo.git) and fetch the PR ref from that remote instead. The refspec pattern refs/pull/*/head only works on the repository where the PR was opened.

How do I update fetched PR refs when new commits are pushed?

Run git fetch origin to update all configured references, including PR refs if you have added the wildcard refspec. Git will update refs/remotes/origin/pr/* to point to the latest commits. If you fetched a single PR into FETCH_HEAD previously, you must run the specific git fetch origin refs/pull/[number]/head command again to see updates.

Is there a difference between checking out FETCH_HEAD and a remote-tracking branch?

Yes. FETCH_HEAD is a temporary reference that updates with every fetch operation, making it unsuitable for long-term work. Checking out pr/42 (a remote-tracking branch) creates a stable reference that you can return to, diff against, and base new branches on. Remote-tracking branches also persist across fetches, while FETCH_HEAD content changes based on the most recent fetch command.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →