Skip to content

Instantly share code, notes, and snippets.

@mnguyenngo
Last active September 20, 2018 22:55
Show Gist options
  • Save mnguyenngo/64d71dfecea8a303c15ad02d73b22b3c to your computer and use it in GitHub Desktop.
Save mnguyenngo/64d71dfecea8a303c15ad02d73b22b3c to your computer and use it in GitHub Desktop.
Clean up remote git branches

Cleaning Up Remote Branches

Objective: Delete remote branches that have already been merged into master

Reference: https://community.atlassian.com/t5/Bitbucket-questions/Is-there-a-way-to-delete-branches-in-bulk/qaq-p/547331

Run the following commands

# Make sure remotes are up to date (with stale remotes purged):
>>> git fetch -p

# Initial no-op run --- do the branches to delete look correct?
# Be careful to omit 'master' from the output.
>>> git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs

# Do the bulk delete!!!  (can take a long time...)
>>> git branch --remote --merged origin/master | grep -v 'master' | cut -b 10- | xargs git push --delete origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment