Skip to content

Instantly share code, notes, and snippets.

@Lzok
Last active July 1, 2019 13:29
Show Gist options
  • Save Lzok/67e3bd867e90305f88978dd4b6f75f2e to your computer and use it in GitHub Desktop.
Save Lzok/67e3bd867e90305f88978dd4b6f75f2e to your computer and use it in GitHub Desktop.
Scripts to delete git branches locally and remotely. Except master, development and others.

Note

The following commands were tested in MacOS. Not yet in Linux.

Delete branches locally

Delete all local branches except master, development and the current branch (in this example). You can add as many branches as you want in the regex.

cd into-the-desired-repo
git branch | egrep -v "(master|development|\*)" | xargs git branch -D

Delete remote branches from origin source

Delete all remote (origin) branches except master, development and the current branch (in this example). You can add as many branches as you want in the regex.

cd into-the-desired-repo
git branch -r | grep origin/ | egrep -v "(master|development|\*)" | grep -v HEAD | cut -d/ -f2- | while read line; do git push origin :$line; done

Delete all local tags in case you need

cd into-the-desired-repo
git tag -d $(git tag -l)

Delete all remote tags in case you need

cd into-the-desired-repo
git push origin --delete $(git tag -l)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment