Last active
May 25, 2019 21:33
-
-
Save chtenb/61e5a3f9f8d0a8fdaaa9f44c601151f8 to your computer and use it in GitHub Desktop.
Python script that lets you archive branches interactively
This file contains hidden or 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
""" | |
Archive branches that are merged into master. | |
This is done by tagging them as archive/<branchname> and removing them both locally and | |
remotely. Before each operation, the user is asked for confirmation. | |
""" | |
# This dependency can be found on github: https://github.com/Chiel92/python-shellout | |
from shellout import get, out, confirm | |
# Tag merged branches | |
merged_branches = [line.strip() for line in | |
get(r'git branch --merged master').n] | |
merged_branches = [branch for branch in merged_branches | |
if branch != '' and not '*' in branch and not branch == 'master'] | |
archived_branches = [] | |
for branch in merged_branches: | |
if confirm('Archive branch {}?', branch): | |
out('git tag archive/{} {}', branch, branch) | |
archived_branches.append(branch) | |
if not archived_branches: | |
exit('No branches archived. Bye.') | |
# Push archive tags to remote | |
print() | |
print('Created archive tags:') | |
for branch in archived_branches: | |
print(' ' + branch) | |
if confirm('Push archive tags to remote?'): | |
for branch in archived_branches: | |
out('git push origin archive/{}', branch) | |
# Delete remote branches | |
print() | |
print('Corresponding remote branches:') | |
for branch in archived_branches: | |
print(' origin/' + branch) | |
if confirm('Delete remote branches?'): | |
for branch in archived_branches: | |
out('git push origin :{}', branch) | |
# Delete local branches | |
print() | |
print('Corresponding local branches:') | |
for branch in archived_branches: | |
print(' ' + branch) | |
if confirm('Delete local branches?'): | |
for branch in archived_branches: | |
out('git branch -d {}', branch) |
Just in case anyone (like me) is looking for the direct link to the script (last updated Jun 16, 2016 at the time of posting this comment):
- https://raw.githubusercontent.com/Chiel92/dotfiles/master/archive_merged_branches.py
- https://github.com/Chiel92/dotfiles/blob/master/archive_merged_branches.py
And to the original article about this script:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist won't be updated anymore. The up-to-date version is in my dotfiles.