How to Checkout Codes from a Fork's Branch for PRs Submitted by External Contributors on GitHub Locally

When handling PRs (Pull Requests) from external contributors on GitHub, you might want to checkout those PR codes to your local machine. This guide covers how to quickly checkout the branch code from the fork repository associated with a PR.

Typically, you might clone the fork repository directly, but there's a more efficient way that doesn't require cloning in a new directory.

To quickly checkout the branch code from a fork repository for a PR, use the following commands:

git fetch origin pull/<ID>/head:<BRANCHNAME>
git checkout <BRANCHNAME>

For practical use, replace <ID> in the command with the actual PR ID, and <BRANCHNAME> with the name you want to assign to the new local branch. Here's an example of how to checkout the code for a PR with ID 47 into a local branch named pr47:

$ git fetch origin pull/47/head:pr47
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 4), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), 3.25 MiB | 862.00 KiB/s, done.
From github.com:mozillazg/XXXX
 * [new ref]         refs/pull/47/head -> pr47

$ git log pr47

$ git checkout pr47
Switched to branch 'pr47'

Comments