Skip to content

Instantly share code, notes, and snippets.

@warvariuc
Created December 24, 2015 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warvariuc/b8c8e1da69780d2162bd to your computer and use it in GitHub Desktop.
Save warvariuc/b8c8e1da69780d2162bd to your computer and use it in GitHub Desktop.
Delete merged branches
#!/usr/bin/env python3
import subprocess
print('Fetching remote branches...')
subprocess.call('git fetch -p --all', shell=True)
print('Processing remote branches...')
remote_branches = subprocess.check_output(
'git branch -r origin/* --merged', shell=True).decode().split('\n')
answer = ''
remote_head = ''
for branch in remote_branches:
branch = branch.strip()
if branch == remote_head:
print('Skipping remote branch %r which is the remote head.' % branch)
continue
if not branch:
continue
remote, branch = branch.split('/', maxsplit=1)
if not remote_head:
remote_head = branch.partition('HEAD -> ')[2]
continue
if 'master' in branch:
print('Skipping remote master branch')
continue
if 'develop' in branch:
print('Skipping remote develop branch')
continue
answer = input('Delete remote branch %r? [Y/n]: ' % branch).upper()
if answer in 'Y':
subprocess.call('git push %s :%s' % (remote, branch), shell=True)
print('Processing local branches...')
local_branches = [branch.strip() for branch in subprocess.check_output(
'git branch --merged', shell=True).decode().split('\n')]
for branch in local_branches:
branch = branch.strip()
if not branch:
continue
if branch == 'master':
print('Skipping master branch')
continue
if branch == 'develop':
print('Skipping develop branch')
continue
if branch.startswith('* '):
print('Skipping current branch')
continue
answer = input('Delete local branch %r? [Y/n]: ' % branch).upper()
if answer in 'Y':
subprocess.call('git branch -d %s' % branch, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment