Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Gone Branch Be Gone
#!/usr/bin/env php
`git fetch -p --all`;
$branches = `git branch -vv`;
preg_match_all('%^\ +(?P<branch>[\w/=\-.#]+)\ +(?P<hash>[0-9a-fA-F]+)\ \[[\w/=\-.#]+:\sgone\]%smx', $branches, $result, PREG_PATTERN_ORDER);
foreach( $result['branch'] as $index => $branch ) {
$output = [ ];
exec("git branch -d " . escapeshellarg($branch) . " 2>&1", $output, $exitCode);
if( !$exitCode ) {
echo "Deleted: {$branch} - was at {$result['hash'][$index]}\n";
} else {
echo "Failed to delete {$branch}\n";
echo "\t| " . implode("\n\t| ", $output) . "\n\n";

This comment has been minimized.

Copy link
Owner Author

@donatj donatj commented Jan 22, 2015

This deletes merged branches that track a remote that no longer exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.