Created
June 22, 2016 07:06
-
-
Save justinmoon/7fbe917c7c064c279bde4980f69ac973 to your computer and use it in GitHub Desktop.
Delete your unused git branches
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
import subprocess | |
from git import Repo | |
def delete_branch(branch): | |
branch_name = branch.name[7:] # chop off "origin/" | |
exit_code = subprocess.call(['git', 'push', 'origin', '--delete', branch_name]) | |
if exit_code != 0: | |
print('problem deleting', branch_name) | |
def main(whitelist): | |
repo = Repo('.') | |
assert len(repo.remotes) == 1, 'too many remotes' | |
origin = repo.remotes[0] | |
throwaways = [b for b in origin.refs if not_whitelisted(b.name, whitelist)] | |
for branch in throwaways: | |
delete_branch(branch) | |
def not_whitelisted(branch_name, whitelist): | |
for keyword in whitelist: | |
if keyword.lower() in branch_name.lower(): | |
return False | |
return True | |
if __name__ == '__main__': | |
whitelist = ['master', 'HEAD'] | |
main(whitelist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment