Created
August 28, 2025 09:33
-
-
Save obnoxxx/e3dadd6506c693b7e236db4ac73c70cd to your computer and use it in GitHub Desktop.
How to fetch github PRs as branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| By adding one simple line to the config file a github repo's local clone, it is possible to fetch pull requests(PRs) as branches, which can be very convenient for reviewers for local testing of PRs and trying to help solve issues. | |
| conceptually, one adds this line to the section `[ remote "origin"]` og `.git/config` in the checkout dir: | |
| ```ini | |
| +refs/pull/*/head:refs/remotes/origin/pr/* | |
| ``` | |
| Then `git fetch origin` will create local branches `origin/pr/NUMBER` locally, e. g. PR number 1 will be available locally as `/origin/pr/1`. | |
| Here is a more complete example for the Rook project: | |
| clone the repo: | |
| ```console | |
| $ git clone +refs/pull/*/head:refs/remotes/origin/pr/* | |
| $ cd rook | |
| ``` | |
| Now `.git/config`looks like this: | |
| ```ini | |
| ... | |
| [remote "origin"] | |
| url = [email protected]:rook/rook.git | |
| fetch = +refs/heads/*:refs/remotes/origin/* | |
| ... | |
| ``` | |
| add a line to the section: | |
| ```ini | |
| ... | |
| [remote "origin"] | |
| url = [email protected]:rook/rook.git | |
| fetch = +refs/heads/*:refs/remotes/origin/* | |
| fetch = +refs/pull/*/head:refs/remotes/origin/pr/* | |
| ... | |
| ``` | |
| now here is an example output of a `git fetch`(after some time of working on the repo: | |
| ```console | |
| $ git fetch origin | |
| remote: Enumerating objects: 5, done. | |
| remote: Counting objects: 100% (3/3), done. | |
| remote: Total 5 (delta 3), reused 3 (delta 3), pack-reused 2 (from 1) | |
| Unpacking objects: 100% (5/5), 576 bytes | 96.00 KiB/s, done. | |
| From github.com:rook/rook | |
| + 4d0dadf82...b79192083 refs/pull/16349/head -> origin/pr/16349 (forced update) | |
| + b67ab45ac...89ce129e1 refs/pull/16388/head -> origin/pr/16388 (forced update) | |
| $ git branch -r | grep pr | |
| ... | |
| origin/pr/9990 | |
| origin/pr/9991 | |
| origin/pr/9992 | |
| origin/pr/9993 | |
| origin/pr/9997 | |
| origin/pr/9998 | |
| origin/pr/9999 | |
| ... | |
| $ | |
| ``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment