Asaduzzaman Pavel

Checkout GitHub Pull Request

GitHub pull requests are a powerful collaboration tool, but managing them efficiently can be a bit challenging. However, there’s a simple trick to make the process smoother. By adding a specific configuration to your Git settings, you can easily fetch and check out pull requests. Let’s walk through the steps to simplify our workflow.

Step 1: Locate Your GitHub Remote Configuration

Open your .git/config file in your project directory. Look for the section related to your GitHub remote. It typically looks like this:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = [email protected]:your-username/your-repo.git

Step 2: Add Fetch Configuration for Pull Requests

Now, add the following line under the remote configuration:

fetch = +refs/pull/*/head:refs/remotes/github-pr/*

Make sure to replace the GitHub URL with your project’s URL. The updated configuration will look like this:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    fetch = +refs/pull/*/head:refs/remotes/github-pr/*
    url = [email protected]:your-username/your-repo.git

Step 3: Fetch All Pull Requests

If we run git fetch, all pull requests should be fetched

 * [new ref]             refs/pull/1000/head -> github-pr/1000
 * [new ref]             refs/pull/1005/head -> github-pr/1005
 * [new ref]             refs/pull/1011/head -> github-pr/1011
 * [new ref]             refs/pull/1015/head -> github-pr/1015

Step 4: Check Out a Specific Pull Request

To check out a particular pull request, use the following command, replacing PR_NUMBER with the actual pull request number:

git checkout pr/PR_NUMBER

For example:

git checkout github-pr/1015

Now, you should be able to view the head of the specific pull request.

Note: switching to 'github-pr/1015'.

You are in 'detached HEAD' state. ...

TLDR

  1. Add fetch = +refs/pull/*/head:refs/remotes/github-pr/* to [remote "origin] in .git/config
  2. Run git fetch
  3. git checkout github-pr/69420