Checkout git branches through your browser
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.
if (!$branchname) {
echo "Please enter a branchname, ?branch=<name>";
return false;
}
Executing the command
We need to cd
into the correct directory and run git checkout $branchname
. We do that using shell_exec()
.
$command = 'cd <directory> && git checkout ' . $branchname;
$output = shell_exec($command . ' 2>&1');
You might not need to change directory, so feel free to remove cd <directory>
. The rest is essential. 2>&1
directs stderr
to stdout
(Or put simply, outputs the result of the command).
Printing the output would be extremely helpful as well:
echo 'Checking out ' . $branchname . '…<br>';
echo $output . '<br>';
And we’re done. Upload it to your server with a filename like checkout-git-branch.php
, and try it out!
Final code
<?php
$branchname = $_GET['branch'];
if (!$branchname) {
echo "Please enter a branchname, ?branch=<name>";
return false;
}
$command = 'cd <directory> && git checkout ' . $branchname;
$output = shell_exec($command . ' 2>&1');
echo 'Checking out ' . $branchname . '…<br>';
echo $output . '<br>';
?>