Checkout a pull request

Greg Foster
Greg Foster
Graphite software engineer

Working with pull requests (PRs) is a way for collaborators to receive context and give feedback on code changes. Sometimes, you need to checkout these PRs locally to review, test, or continue development. This guide explains how to checkout pull requests in Git from different remotes using both the command line and GitHub CLI approaches.

A pull request in platforms like GitHub is a way to request that another branch be merged into a base branch, usually from a fork or another branch within the same repository. PRs are central to collaborative projects, allowing for code review and discussion before changes are integrated into the main project.

For repositories like those on GitHub, you can configure Git to fetch all of the pull requests from the original repository (upstream) by modifying your Git configuration:

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

This command updates the fetch configuration for the origin remote, instructing Git to fetch pull request data into a new ref namespace, pr/*. Here’s what each part means:

  • +refs/pull/*/head: This specifies that Git should fetch the head (the tip of the branch) of all pull requests.
  • refs/remotes/origin/pr/*: This tells Git to store these heads in a local ref path that can be checked out easily.

This is useful if you want to checkout and view many PRs in sequence.

Once you've configured Git to recognize pull requests:

Terminal
git fetch origin

This command pulls all pull request data into your local repository under the refs specified earlier.

After fetching the pull requests, you can checkout a specific one by referring to its PR number:

Terminal
git checkout pr/123

Replace 123 with the actual pull request number. This command switches your working directory to the state of the pull request.

If you use GitHub and have the GitHub CLI installed, you can checkout pull requests directly without configuring the fetch settings:

  1. List open pull requests:

    Terminal
    gh pr list
  2. Checkout a pull request by number:

    Terminal
    gh pr checkout 123

    Replace 123 with the pull request number. The GitHub CLI handles the fetching and checking out steps for you.

With the PR checked out locally, you can now compile the code, run tests, or do manual testing to ensure that the changes meet the project's standards and do not introduce bugs.

  • Keep your local and remote branches updated: Regularly pull changes from the main branch of your repository to keep your PR branches up-to-date and avoid conflicts.

  • Review changes thoroughly: Use the local checkout to perform a thorough review of the pull request changes. Look for code quality, functionality, and integration with existing code.

  • Clean up after testing: Once you're done testing or have merged the PR, switch back to your main branch and delete the local PR branch if necessary:

    Terminal
    git checkout main
    git branch -D pr/123 # Delete the local PR branch

For further reading on checking out pull requests, see the official GitHub documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2