What I do is leaving PackagesLocalDirectory without any custom code. I have a separate folder for a repository, where I have folders for different branches. Each of this has its own workspace. I sometimes even have multiple folders (and workspaces) for the same branch. Typically, I need to do a code review and I don't want to unshelve the changes to my primary development workspace, therefore I have two workspaces for the DEV branch (one for development and one for review).
Note that I also have Visual Studio projects under branch folders.
When I want to switch a branch, I run a script to reconfigure web.config and DynamicsDevConfig.xml.
But there is an extra step needed and it may be a bit confusing. If I tried to use a metadata folder that contains just code that I get from source control, I would be missing all the standard stuff, such as Application Platform module. It couldn't work. I could copy the content of PackagesLocalDirectory to each branch, but it would be very slow and time-consuming. And I would have to do it again after installing an update of standard code. A better solution is creating symblic links (by a script, of course) to standard packages, which can be done in a second and it always point out to latest application.
Here is a Powershell script to create symbolic links:
$branchMetaDir = 'k:\Repo\Dev\Metadata'
$packageDir = 'k:\AosService\PackagesLocalDirectory'
# For each folder in packages (it assumes that there are no custom packages)
foreach ($dir in (ls $packageDir -dir))
{
$targetDir = (Join-Path $branchMetaDir $dir.Name)
if (Test-Path $targetDir)
{
#If there is a already a folder of the same name as the symblic link, delete it
cmd /c rmdir /s /q $targetDir
}
# Create a symbolic link
# It assumes that the Metadata subfolder already exists
New-Item -ItemType SymbolicLink -Path $targetDir -Target $dir.FullName
}