Here is a script I made to delete local and remote branches merged into the current HEAD:
#!/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 answer != 'A':
answer = input('Delete remote branch %r? (y/a or Enter) ' % branch).upper()
if answer in 'YA':
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.startswith('* '):
print('Skipping current branch')
continue
if answer != 'A':
answer = input('Delete local branch %r? (y/a or Enter) ' % branch).upper()
if answer in 'YA':
subprocess.call('git branch -d %s' % branch, shell=True)
No comments:
Post a Comment