I'm using GitHub to host some projects and someone forked my repo and submitted a PR. I have never had to test a PR in a fork before. How can I checkout the branch and test it? Can I some how pull it into my repo? Or do I checkout the fork and test the branch that way?
You don't need access to the fork. Just fetch it from the main repo using the PR ref. If this is PR 3
, then fetch pull/3/head
like this:
git fetch origin pull/3/head:my_new_branch
git checkout my_new_branch
Basically the "upstream" GitHub repository provides direct access to the forks' PR branches (in read-only) so that you can fetch a given PR and test the corresponding code after specifying the appropriate ref, pull/ID/head
.
To this aim, I often use the following alias (which can be used for both initial fetch, then subsequent fetches if the PR has been updated−thereby overwriting the pr/ID
local branch):
$ git pr
Usage: git pr <id> [<remote>] # assuming <remote>[=origin] is on GitHub
$ git pr 101 # fetch PR #101 from origin -> branch pr/101
$ git pr 101 upstream # fetch PR #101 from upstream -> branch pr/101
Disclaimer: this definition is not the very last version of the command, see
Further automation
below.
Here is the one-liner definition, then an expanded form:
$ git config --global alias.pr '!f() { if [ $# -lt 1 ]; then echo "Usage: git pr <id> [<remote>] # assuming <remote>[=origin] is on GitHub"; else git checkout -q "$(git rev-parse --verify HEAD)" && git fetch -fv "${2:-origin}" pull/"$1"/head:pr/"$1" && git checkout pr/"$1"; fi; }; f'
# git config --global alias.pr '!f() { if [ $# -lt 1 ]; then
echo "Usage: git pr <id> [<remote>] # assuming <remote>[=origin] is on GitHub";
else git checkout -q "$(git rev-parse --verify HEAD)" &&
git fetch -fv "${2:-origin}" pull/"$1"/head:pr/"$1" &&
git checkout pr/"$1";
fi; }; f'
Note however that you cannot directly push to these special branches.
If ever you'd like to directly modify the PR (commit+force-push) as a maintainer, you should then add the fork as another remote
, as suggested by @yes-siz (provided the PR author authorized such access when opening their PR).
It can be noted that the git pr
alias above creates a read-only branch, because being able to force-push to the branch that is associated with the PR would require to push to a specific branch within the source fork repository (assuming its owner authorized this access, as mentioned previously).
Fortunately, GitHub's REST API allows one to easily retrieve this information (the GitHub source repository as well as the source ref), so that one can automate this by relying on a simple curl
call and a few standard Git commands including git branch --set-upstream-to
.
All things put together, we can obtain some handy Bash command:
$ git prw
Facility to fetch a read/write branch from a GitHub repo Pull Request.
Usage:
git prw <ID> [<REMOTE>] [-f]
Example:
git prw 390 upstream
Summary:
If the REMOTE argument is omitted, it defaults to "origin".
REMOTE must point to a GitHub repository.
This command checkouts the branch named "pr/ID" (if it doesn't exist
yet, it fetches the source branch for the PR #ID in the REMOTE repo)
and sets its upstream branch so that one can force-push to the fork
(using an SSH URL); it reuses (if applicable) an existing remote
matching that URL, or creates a remote named REMOTE-fork-for-pr-ID.
Flag -f overwrites the local branch pr/ID even if it already exists.
In general, it is a good idea to pass flag -f, unless we already ran
"git pr ID REMOTE" and did commits in the local branch pr/ID.
It requires curl <https://curl.se/> and (optionally) jq.
See also:
- https://stackoverflow.com/a/62432946/9164010
- https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally
- https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork
I published this git-prw
command in my git-scripts
GitHub repository (given it is not a one-liner script any longer :) and this repository also contains an up-to-date version of the git-pr
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With