Skip to content

Instantly share code, notes, and snippets.

@phpfunk
Last active August 29, 2015 14:23
Show Gist options
  • Save phpfunk/63965dbe5852911ab68e to your computer and use it in GitHub Desktop.
Save phpfunk/63965dbe5852911ab68e to your computer and use it in GitHub Desktop.
Delete local branches not on remote
#!/usr/bin/php
<?php
define('REMOTE_NAME', 'jeff');
$branchesToKeep = array(
'cas', 'integration-sdk', 'saml-testing', 'new-authentication'
);
$branchCommands = array(
'local' => 'git branch',
'remote' => "git ls-remote --heads " . REMOTE_NAME . " | sed 's?.*refs/heads/??'"
);
$localBranches = array();
$remoteBranches = array();
foreach ($branchCommands as $location => $command) {
$branches = explode(PHP_EOL, shell_exec($command));
foreach ($branches as $branch) {
$branch = trim(str_replace('*', '', $branch));
if (empty($branch)) {
continue;
}
if ($location === 'local') {
$localBranches[] = $branch;
} else {
$remoteBranches[] = $branch;
}
}
}
foreach ($localBranches as $k => $branch) {
if (!in_array($branch, $remoteBranches)
&& !in_array($branch, $branchesToKeep)) {
print shell_exec('git branch -D ' . $branch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment