Skip to content

Commit

Permalink
Add script to cleanup dead remote branches
Browse files Browse the repository at this point in the history
  • Loading branch information
ajbonner committed Jan 1, 2017
1 parent 99a0f84 commit 20dab39
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bin/git-branch-clean.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash
set -e
set -o pipefail

if [ $# -lt 3 ]; then
echo "Usage: $0 path/to/keep.txt git@somerepo.com/repo.git\n"
exit 1
fi

IGNORE_FILE=$1
REPO=$2

if [ ! -r $IGNORE_FILE ]; then
echo "keep.txt not found - exiting..."
exit 1
fi

REMOTE_BRANCHES=$(git ls-remote $REPO | awk '{print $2}' | sed 's#refs/heads/##' | grep -v '^HEAD$')
KEEP_BRANCHES=$(cat $IGNORE_FILE)

for REMOTE_BRANCH in ${REMOTE_BRANCHES[@]}; do
if [[ " ${KEEP_BRANCHES[@]} " =~ "${REMOTE_BRANCH}" ]]; then
echo "Keeping $REMOTE_BRANCH"
else
echo "Deleting $REMOTE_BRANCH"
git push origin :$REMOTE_BRANCH
fi
done

0 comments on commit 20dab39

Please sign in to comment.