Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I checkout a PR from a fork?

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?

like image 434
Arnold Rimmer Avatar asked Aug 31 '25 17:08

Arnold Rimmer


2 Answers

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
like image 133
jschmitter Avatar answered Sep 03 '25 15:09

jschmitter


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).

Further automation

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.

like image 28
ErikMD Avatar answered Sep 03 '25 15:09

ErikMD