Checkout a branch from a fork

A common thing that I do is to test out pull requests to ensure that the proposed changes are working and don’t introduce any regressions before merging them. My workflow for doing this is often to just checkout the specific branch and start Atom in dev mode since I already have many of the packages linked in Atom or a fresh build of master. However when someone from the community opens a pull request from their fork of one of our repositories I always clone their fork to another directory and in the case of core packages I relink this to Atom. This eventually causes me to have a mess of folders all for the same repository and there are quite many steps involved to get it up and running for testing. For pull requests to atom/atom this takes a long time because it involves rebuilding Atom entirely in the new clone.

Is there some way I could improve my workflow to checkout the branch directly to the clone I already have even when the branch is created on a fork so I don’t have to clone every time I want to test out a PR?


This is a great question!

For clarity, you’re wanting to test a PR to a GitHub repository that you own or maintain, let’s call it ben3eee/some-repo. That PR was submitted by someone who does not have push access to the repository, so they submitted the PR from a fork of the repository that you own or maintain. We’ll call that fork contributor/some-repo. You want to take a look at their changes in your local copy of ben3eee/some-repo as a branch.

First of all, you can find the most up-to-date information about this in the GitHub Help under Checking out pull requests locally. But for a little more background, when someone submits a PR, a Git reference (or “ref”) is created that stores the reference to those commits in the repository on GitHub. You can see this if you run the following in your local repo:

git ls-remote --refs origin

You’ll see all of the refs on the remote named origin, including some number of refs in the format refs/pull/123/head. In the case of refs/pull/123/head, that is a reference to the head of the branch for PR #123 in the repository. Knowing that GitHub stores that information this way, you can fetch that ref to your local repository and check out the branch with the following command:

git fetch origin pull/123/head:pr/123 && git checkout pr/123

You will then be on a local branch named pr/123 containing the code in PR #123. For my personal use, I’ve created an alias in my Git configuration to make the process simpler so I can type:

git co-pr 123

to achieve the same effect.

Since I wrote the above over two years ago, GitHub has since released the GitHub CLI tool:

When using the GitHub CLI, the same process above can be achieved using the command:

gh pr checkout 123

Thank you so much for this answer :heart: The alias in the git configuration looks great!

1 Like

Thanks, @lee-dohm. This is very helpful for project maintainers. By the way, could you also provide any best practices regarding the branching a maintainer should follow (viz contributors to submit PR’s etc.)? Please provide the practices that are widely used on GitHub. :smiley:

The best practices that we recommend is summarized as the GitHub Flow. We also created an illustrated guide that many find helpful. Beyond that, different projects have different needs and there is a myriad of branching strategies as a result. They all are based on the common theme of the GitHub Flow though, so that’s where we recommend people start and experiment from there :+1:


macOS version of the above alias:

copr() { git fetch origin "refs/pull/$1/head:pr/$1" && git checkout "pr/$1"; }
alias co-pr="copr"

Can I push to that reference?