Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delete merged branches with origing/master
#/bin/sh
# Configurable
remote='origin';
master='master';
days=0;
unmerged=0;
eval set -- `getopt -l master: -l remote: -l days: -l help -l unmerged -- 'm:?r:?d:?u?h?' "$@"`;
while [ "X$1" != "X--" ]; do
case "$1" in
-m|--master)
shift
master=$1;
;;
-r|--remote)
shift
remote=$1;
;;
-d|--days)
shift
days=$1;
;;
-u|--unmerged)
unmerged=1;
;;
-h|--help)
echo `basename $0`' [-m|--master=master] [-r|--remote=origin] [-d|--days=days] [-u|--unmerged] [-h|--help]';
exit 0;
;;
esac
shift
done
shift;
# Needed
latest='dgaghdfjkyhier32583459354';
prefix='73258654fdsfds';
# Program
git fetch -p ${remote};
git branch ${latest} ${remote}/${master} >/dev/null 2>&1;
now=`date +'%s'`;
git branch -a |
grep remotes/${remote} |
grep -vE '/'${master}'$' |
sed -e 's/remotes\///' |
while read b; do
c=`echo "$b" | sed -e 's/'${remote}'\///'`;
git branch ${prefix}/$c $b >/dev/null 2>&1;
d=`git branch --merged ${latest} | grep ${prefix}/$c`;
ts=`git show -q --pretty=%at ${prefix}/$c`;
ago=$(( (${now} - ${ts}) / (60 * 60 * 24) ));
if [ ${unmerged} -eq 0 ] && [ -n "$d" ]; then
if [ ${ago} -ge ${days} ]; then
echo "git push ${remote} :$c # $ago days ago";
fi
elif [ ${unmerged} -eq 1 ] && [ -z "${d}" ]; then
if [ ${ago} -ge ${days} ]; then
echo "git checkout -tb $c ${remote}/$c # $ago days ago";
fi
fi
git branch -d ${prefix}/$c > /dev/null 2>&1;
done;
git branch -d ${latest} >/dev/null 2>&1;
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.