Last active
December 3, 2015 19:12
-
-
Save donatj/8ab68d3128e6a51024e8 to your computer and use it in GitHub Desktop.
Gone Branch Be Gone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This deletes merged branches that track a remote that no longer exists.