Checkout git branches through your browser

15 Mar 2014

Most git workflows involve use of multiple branches for different sub-tasks, example, a new branch for an alternative layout for the homepage. However, managing braches on the server quicky gets tedious - SSHing in, navigation to the correct directory, then running git checkout <branch> - is tiring for all, right?.

That's why I came up with a simple solution that used PHP and GET requests to checkout different branches on the server through the browser.

The Concept

What we're trying to achieve here is:

An easy way to pass a branch name to a script.

That script uses that branch name to run a checkout in the correct directory.

The output of the command is presented to us, to tell if it ran correctly or not.

Doing this with a small, but powerful, PHP script is our challenge.

The Code

GET Request

We'll be passing variables as GET requests, because:

It's easy.

It's lazy.

So just make a variable holding the GET variable in your PHP

$branchname = $_GET['branch'];

We'll also need to check if the user has actually supplied a request, if not, echo a helpful message and stop the script from executing further.