Checkout a pull request (PR) on a repository that you have forked
When you start working in Open-Source, one of the incredible ways to help out is by testing and reviewing PRs.
Since I've been maintaining the Codú platforms codebase, I often have to checkout PRs locally, and I know it confused me when I started, so hopefully this verbose example helps.
This guide assumes you have forked the codebase and locally cloned the fork.
Make sure you set your upstream.
If you have already set up your upstream, skip to the next section.
To get changes from the parent repository that you forked from you need to make sure you have it set as upstream.
The following is from a GitHub guide (where you can find here).
List the current configured remote repository for your fork.
$ git remote -v > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Verify the new upstream repository you've specified for your fork. You should see that your upstream is set correctly by rerunning the
git remote -v command.
$ git remote -v > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) > origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) > upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch) > upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
And an example of the output from my config from this repo.
$ git remote -v > origin firstname.lastname@example.org:NiallJoeMaher/codu.git (fetch) > origin email@example.com:NiallJoeMaher/codu.git (push) > upstream https://github.com/codu-code/codu.git (fetch) > upstream https://github.com/codu-code/codu.git (push)
Checking out a PR on the parent repository
The first thing you need to do is fetch the changes in the PR.
Run the following command to fetch the changes from the upstream repository:
git fetch upstream pull/ID/head:BRANCHNAME
ID with the number of the pull request and
BRANCHNAME with the name of the PR branch you are trying to checkout.
You should now have a local branch with the changes from the pull request that you can review, test, and alter locally if you wish.
As a real-world example from a PR currently open on Codú as I'm writing this. 👇
$ git fetch upstream pull/163/head:dancallaghan/user_prompt_unsaved_changes_issue11
Then checkout the branch:
$ git checkout dancallaghan/user_prompt_unsaved_changes_issue117 > Switched to branch 'dancallaghan/user_prompt_unsaved_changes_issue117'
Happy coding. 👨🏻💻