Locally Test Pull Requests

In this post, I am going to cover how to easily test pull requests on your local machine. When you work in a team where people are submitting pull requests, you want to make sure their changes are free of bugs and do not break any existing code in the master branch before the pull requests are merged. If you've been manually copying and pasting new code from pull requests into your local environment to test, then you really need to read this post. You want to test all code changes, and you don't want to have to copy and paste if there are a lot of files that changed.

This post is written in the context of using a GitHub workflow.


I'm also going to assume you have upstream and origin remotes set up in your local repository. upstream should point to the main repo that is shared by the team and origin should point to your personal (forked) repo. If you are not familiar with this concept, check out this tutorial.

Let's get started

Let's say this is the most recent tip of the current master in upstream as well as on your local machine:

*   d4138b6 2015-05-15 | Merge pull request #8 from aapple/feat/resize-method (HEAD, upstream/master, origin/master) [Billy Banana]
| * 7e31ce9 2015-05-15 | Add resize method [Adam Apple]
*   9a24fe0 2015-05-13 | Merge pull request #7 from bbanana/feat/insert-method [Cathy Cherry]

And let's say Denise Date is working on a new feature and has submitted a pull request. Denise followed best practice and did her work in a feature branch on her local machine and then pushed to her GitHub forked repo. Her feature branch is called feat/remove-method.

So now, we want to pull down her changes and do a code review and make sure everything is fine with the new changes. From your local machine, you want to do the following:

  • Create a remote for Denise's repo if it doesn't already exist with git remote add denise <REPO URL>
  • Make sure you are on master by using git checkout master
  • Create a branch for testing Denise's new code by using git checkout -b feat/remove-method
  • Pull down Denise's code into the branch with git pull --rebase denise feat/remove-method
  • Test test test

For the git pull into the feature branch, you don't technically need the --rebase, but it's good to use to have a cleaner commit history since it will not create a merge commit. Denise's code should now be combined with the existing codebase.

Now from the feat/remove-method branch in your local repo, the commit history will look something like this:

* cafd1a5 2015-05-14 | Add remove method (HEAD, denise/feat/remove-method, feat/remove-method) [Denise Date]
*   d4138b6 2015-05-13 | Merge pull request #8 from aapple/feat/resize-method (HEAD, upstream/master, origin/master, master) [Billy Banana]
| * 7e31ce9 2015-05-12 | Add resize method [Adam Apple]
*   9a24fe0 2015-05-11 | Merge pull request #7 from bbanana/feat/insert-method [Cathy Cherry]

After reviewing her code and making sure everything works, when the pull request is merged into upstream/master, you should also make sure your local and fork master branch are synchronized with upstream.