Skip to content

Instantly share code, notes, and snippets.

@benfortuna
Last active May 2, 2022 05:19
Show Gist options
  • Save benfortuna/45f51efc6f441eb0ab771af8bb208994 to your computer and use it in GitHub Desktop.
Save benfortuna/45f51efc6f441eb0ab771af8bb208994 to your computer and use it in GitHub Desktop.
Cleanup stale git branches
#!/bin/bash
# list merged branches
BRANCHES=`git branch -r --merged | grep -v "*" | grep -v master | grep -v develop | grep -v release/ | grep -v hotfix/ | sed 's/origin\///'`
printf "Removing branches: \n$BRANCHES"
# delete all remote branches merged into the current branch (i.e. develop), excluding release/master/develop branches
git branch -r --merged | grep -v "*" | grep -v master | grep -v develop | grep -v release/ | grep -v hotfix/ | sed 's/origin\///' | xargs -n 1 git push --delete origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment