Skip to content

Instantly share code, notes, and snippets.

@christopher-hopper
Created June 7, 2019 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopher-hopper/4bce7f0be849a678c9ca1671aa12d916 to your computer and use it in GitHub Desktop.
Save christopher-hopper/4bce7f0be849a678c9ca1671aa12d916 to your computer and use it in GitHub Desktop.
Delete remote branches that have been merged to master.
#!/usr/bin/env bash
# vim: ai ts=2 sw=2 et sts=2 ft=sh
# Delete remote branches that have been merged to master.
#
# Usage:
#
# git-remote-merged.sh -f
#
# Bash strict mode.
set -o pipefail
set -o errexit
set -o nounset
#IFS=$' '
_force=false;
while getopts ":fn" opt; do
case ${opt} in
n ) _force=false;
;;
f ) _force=true;
;;
\? ) echo "Usage: $(basename "${BASH_SOURCE[0]}") [-n] [-f]"
;;
esac
done
_git_opts='-n --delete'
if [[ "$_force" == true ]]; then
_git_opts='--delete'
fi
for branch in $(git branch -r --merged master | grep "origin" | grep -v "develop" | grep -v "master" | grep -v "release");
do
if [[ "$_force" == false ]]; then echo "Would remove ${branch}"; fi
git push origin $_git_opts "${branch##*/}";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment