Check out someone else's pull request using the GitHub CLI

Checking out someone else's pull request (PR) on GitHub is essential in collaborative software development, enabling thorough code reviews, local testing, and detailed feedback. This process allows developers to ensure the quality and functionality of the code before integration, identify and resolve potential conflicts, and maintain consistency across the project. By reviewing changes locally, team members can collaborate more effectively, offering insights and improvements that enhance the overall quality of the project. Additionally, local reviews support security audits and performance evaluations, ensuring that the code is not only functional but also optimized and secure.

Performing a code review

This process allows you to fetch the changes made in someone else's PR and review them locally in your development environment. Follow these steps to check out a pull request (PR) using the GitHub CLI (gh).

  1. Install the GitHub CLI: First, ensure that the GitHub CLI is installed on your machine. If it's not installed, you can download it from the GitHub CLI page.

    You can also install the GitHub CLI using a package manager like Windows Package Manager (winget) on Windows:

    1winget install --id GitHub.cli
    

    Or Homebrew (brew) on macOS:

    1brew install gh
    

    For more installation options, refer to the GitHub CLI installation guide.

  2. Authenticate: If you haven't already authenticated the GitHub CLI with your GitHub account, you can do so by running:

    1gh auth login
    
  3. Clone the repository: If you haven’t cloned the repository that contains the PR, you need to do so. You can clone the repository using the gh repo clone command followed by the repository URL:

    1gh repo clone <repository URL>
    
  4. Navigate to your repository: Change the directory in your terminal to the local Git repository that's linked to the GitHub repository containing the PR:

    1cd path/to/your/repository
    
  5. Fetch and Checkout the Pull Request: Use the gh pr checkout command followed by the PR number to check out the PR. The PR number is on the GitHub repository's Pull Requests page.

    The following command fetches the branch associated with the pull request and switches to it in your local repository. You can then review the changes, run tests, or integrate them with your local codebase.

    1gh pr checkout <PR number>
    

    One trick Sean Wheeler shared with me is that you can click the Code button when you have a PR open on the GitHub website. The exact gh command you need, including the PR number, is listed in the Checkout with GitHub CLI section of the Local tab, as shown in the following image.

    Checkout PR with GitHub CLI

    If you encounter any issues or need to look up more options for the command, you can always refer to the GitHub CLI manual by typing gh pr --help in your terminal.

Summary

In this blog article, you've learned how to check out someone else's pull request using the GitHub CLI. This process allows you to fetch the changes made in their PR and review them locally in your development environment. The GitHub CLI provides a convenient way to interact with GitHub from the command line, making it easier to manage pull requests and other GitHub features.

References