GIT Hack: Test Any Open Source Pull Request in Seconds

Have you ever taken a look at the list of Pull Requests on the Ghost repo and wished it was easier to try them out? The following post details how to setup your environment so that testing a PR is as easy as:

$ pr 3361

Step 1: Checking out PRs

This step comes from a gist by Bert Belder, a.k.a @piscisaureus:

You need to edit the git config file for your locally checked out Ghost repository. To do this locate the file .git/config inside your local clone and open it for editing. Something like:

$ cd Ghost && vim .git/config

Inside your git config file, you should see an entry for the remote Ghost repo which looks a bit like this:

[remote "upstream"]
  url = [email protected]:TryGhost/Ghost.git
  fetch = +refs/heads/*:refs/remotes/upstream/*

We're going to add a second fetch line to this block, as shown here:

[remote "upstream"]
  url = [email protected]:TryGhost/Ghost.git
  fetch = +refs/heads/*:refs/remotes/upstream/*
  fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*

Tip: If you're using the vim editor, you'll need to type i for insert mode, so that you can edit the file and use your keyboard to navigate aorund.

Go ahead and add this line, save and exit the file. git fetch upstream will now fetch any new PRs as a branch called pr/####. Typing git checkout pr/3361 will switch you onto a branch containing the changes from PR #3361.

From here you will still need to run a set of commands to get Ghost running, including npm install, grunt init and npm start. This can still be easier...

Tip: If you've read all this and gotten yourself stuck in the vim editor, you need to type ESC : wq Enter to save and exit ;)

Step 2: Bash alias

Bash aliases let you create your own name for a command or set of commands, so by adding a bash alias, we can make all of this as easy as typing:

$ pr 3361

Setting up a bash alias takes just a few seconds, and will save you a lot of time and energy remembering and typing arcane strings of commands.

You need to open your bash config file for editing. If you use git bash on Windows (which I can't live without) the file you need to edit will likely be ~/.bashrc. For terminal on Mac the code is the same but the file you'll need to edit is usually ~/.bash_profile. In both cases ~ means your user directory.

Go ahead and find your bash config file, and open it for editing. Something like

$ vim ~/.bashrc

Paste the following code anywhere in that file:

alias pr='f() { git fetch upstream && git checkout pr/"$1" && npm install && grunt init && npm start; }; f'  

If you work on multiple projects, whilst you're here perhaps change the alias to ghostpr rather than just pr, and setup a similar command for your other projects?

To finish up, save and exit the bash config file. Your new command won't work until you reload the config by either opening a new terminal / bash window or by executing it with a command like this (notice the dot):

$ . ~/.bashrc

Now you can easily test Ghost pull requests!