Skip to content

Instantly share code, notes, and snippets.

@justinmoon
Created June 22, 2016 07:06
Show Gist options
  • Save justinmoon/7fbe917c7c064c279bde4980f69ac973 to your computer and use it in GitHub Desktop.
Save justinmoon/7fbe917c7c064c279bde4980f69ac973 to your computer and use it in GitHub Desktop.
Delete your unused git branches
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