How to check out a GitHub pull request locally

A common git workflow is for developers to work on a feature branch of their local fork and then submit a pull request to the upstream master.

The pull request is then reviewed by other members of the team and if everything looks good it's merged in.

For simple pull requests, you can read the code line for line and know whether or not anything might be broken.

For more complicated pull requests, you might like to actually pull down the code and test it.

There are a couple of solutions.

You could clone the repo of the developer who submitted the request.

You could modify your .git/config file.

But my favorite solution is to add this function to my .bash_profile:

function gitpr() {
  # gitpr <remote repo> <pull request number> <new branch name>
  # fetches the PR and stores it in branchname so you can view it locally
  git fetch $REMOTE pull/$ID/head:$BRANCHNAME
  git checkout $BRANCHNAME