I’ve recently switched to a rather rad workflow involving a local copy of our website run through MAMP using a git repository.

We’ve recently started working on a bigger set of changes that require a feature branch. But, these changes also will require WordPress to be upgraded to 3.3, requiring database changes.  Making it impossible to use the same database.

I was able to come up with this nifty technique for determining which DB to use.

$stringfromfile = file('.git/HEAD', FILE_USE_INCLUDE_PATH);
$stringfromfile = $stringfromfile[0]; //get the string from the array
$explodedstring = explode("/", $stringfromfile); //separate out by the "/" in the string
$branchname = trim($explodedstring[2]); //get the one that is always the branch name
 
// ** MySQL settings - You can get this info from your web host ** //
if($branchname == "master")
{
	/** The name of the database for WordPress */
	define('DB_NAME', 'wordpress_local');
}
else
{
	define('DB_NAME', 'wordpress_new');
}

That’s all there is to it. It works flawlessly.

Happy coding.