Skip to content

Instantly share code, notes, and snippets.

@donatj
Last active December 3, 2015 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save donatj/8ab68d3128e6a51024e8 to your computer and use it in GitHub Desktop.
Save donatj/8ab68d3128e6a51024e8 to your computer and use it in GitHub Desktop.
Gone Branch Be Gone
#!/usr/bin/env php
<?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";
}
}
@donatj
Copy link
Author

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