Skip to content

Instantly share code, notes, and snippets.

@NicholasTD07
Last active October 1, 2015 07:36
Show Gist options
  • Save NicholasTD07/12302a59fb1515e8dbc0 to your computer and use it in GitHub Desktop.
Save NicholasTD07/12302a59fb1515e8dbc0 to your computer and use it in GitHub Desktop.
Remove all merged branches
#!/usr/bin/env python2
# Remove all merged branches, except ignored as specified in IGNORE_BRANCHES
import subprocess
IGNORE_BRANCHES = ['master', 'develop']
def main():
checkout_branch('master')
merged_branches_except_ignored = filter_branches_with_ignores(
merged_branches(),
IGNORE_BRANCHES
)
map(delete_branch, merged_branches_except_ignored)
def checkout_branch(branch):
subprocess.call(['git', 'checkout', branch])
def merged_branches():
merged_branches = subprocess.check_output(['git', 'branch', '--merged']).splitlines()
return [ branch.strip() for branch in merged_branches ]
def filter_branches_with_ignores(branches, ignores):
return [ branch for branch in branches
if not any( # if the branch does not contain any part of ignored branches
map(lambda ignore: ignore in branch, ignores)
)
]
def delete_branch(branch):
print(subprocess.check_output(['git', 'branch', '-d', branch]))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment