Last active
April 8, 2021 13:44
-
-
Save shazron/87966896c72e3197f5391b13fd0d7a14 to your computer and use it in GitHub Desktop.
Delete all Greenkeeper branches
This file contains 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
#!/bin/bash | |
# Description: | |
# Delete all `greenkeeper/*` branches of your remote. | |
# Instructions: | |
# Run the script with the `--help` flag. | |
ORIGIN=origin | |
DRY_RUN=0 | |
THIS=`basename "$0"` | |
while test $# -gt 0; do | |
case "$1" in | |
-d|--dry-run) | |
shift | |
DRY_RUN=1 | |
shift | |
;; | |
-h|--help) | |
shift | |
echo "$THIS [REMOTE] [--dry-run]" | |
echo "REMOTE (optional) - the git remote, defaults to 'origin'" | |
echo "--dry-run (optional) - shows what branches that will be deleted," | |
echo " without actually deleting them" | |
echo "--help (optional) - this help message" | |
exit 1 | |
;; | |
*) | |
ORIGIN=${1:-origin} | |
shift | |
;; | |
esac | |
done | |
if [[ $DRY_RUN -eq 1 ]]; then | |
git ls-remote --heads $ORIGIN | sed 's?.*refs/heads/??' | grep "^greenkeeper/" | |
else | |
git ls-remote --heads $ORIGIN | sed 's?.*refs/heads/??' | grep "^greenkeeper/" | xargs -n 1 git push --delete $ORIGIN | |
fi |
@steffenz I updated the script with a --dry-run
option
Wow - that's awesome 😄 Wasn't expecting a fix at all, but it is definitely highly appreciated. Thanks for sharing, and making it even better 🎉 You saved me a lot of time!
FYI, I have a GitHub action for it: https://github.com/cometkim/cleankeeper
👍 Nice :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I recommend people stumbling across this to run the command without the last xargs pipe first, just to get a glance of which branches would potentially be affected (there is no easy way to reverse this as far as I'm concerned). For somebody with 300+ branches to clean up in order to remove and replace greenkeeper, this is a real life-saver. Thanks 👍