Skip to content

Instantly share code, notes, and snippets.

@RaviTezu
Forked from leoapost/gist:4318441
Last active March 29, 2018 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RaviTezu/b7dbaadc434bf1a0942ab26e17853760 to your computer and use it in GitHub Desktop.
Save RaviTezu/b7dbaadc434bf1a0942ab26e17853760 to your computer and use it in GitHub Desktop.
Delete all remote branches, except master on your fork | github.
#!/usr/bin/env bash
set -e
# The MIT License (MIT)
# Copyright (c) 2018 RaviTezu
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
function usage {
printf "./remove_except_master.sh -r <remote_name> \n"
printf "Example: ./remove_except_master.sh -r origin \n"
exit 1
}
while getopts ":r:" opt; do
case $opt in
r) REMOTE_NAME="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
echo
usage
;;
esac
done
function go_killing {
# Check if the remote is set.
if [ -z "$REMOTE_NAME" ]; then
usage
fi
# Check if you are in a git repository or not.
YES_GIT=`git rev-parse --is-inside-work-tree`
if [ "$YES_GIT" == "true" ]; then
git branch -r | grep $REMOTE_NAME/ > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Grrrr.. No such remote exists. Bye!"
exit 1
else
for branch in $(git branch -r | grep -v 'master$'| grep -v HEAD| cut -d/ -f2); do
# "|| true" will make the script not exit if the branch doesn't exist on your fork.
git push $REMOTE_NAME :$branch || true
done
fi
else
echo "Grrr... This is _not_ a GIT repository. Bye!"
exit 1
fi
}
echo "...... [ Going for a kill ]"
go_killing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment