Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.