Skip to content

Instantly share code, notes, and snippets.

@shazron
Last active April 8, 2021 13:44
Show Gist options
  • Save shazron/87966896c72e3197f5391b13fd0d7a14 to your computer and use it in GitHub Desktop.
Save shazron/87966896c72e3197f5391b13fd0d7a14 to your computer and use it in GitHub Desktop.
Delete all Greenkeeper branches
#!/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
Copy link

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 👍

@shazron
Copy link
Author

shazron commented Mar 13, 2020

@steffenz I updated the script with a --dry-run option

@steffenz
Copy link

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!

@cometkim
Copy link

FYI, I have a GitHub action for it: https://github.com/cometkim/cleankeeper

@nightmareze1
Copy link

nightmareze1 commented Apr 1, 2020

👍 Nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment