Skip to content

Instantly share code, notes, and snippets.

@pgib
Created January 23, 2023 22:56
Show Gist options
  • Save pgib/b0c68d7c4cd7113975809e71f0fa11a1 to your computer and use it in GitHub Desktop.
Save pgib/b0c68d7c4cd7113975809e71f0fa11a1 to your computer and use it in GitHub Desktop.
rmb()
{
current_branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [ "$current_branch" != "master" ]; then
echo "WARNING: You are on branch $current_branch, NOT master."
fi
echo "Fetching merged branches..."
git remote prune origin
remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$")
local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$")
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then
echo "No existing branches have been merged into $current_branch."
else
echo "This will remove the following branches:"
if [ -n "$remote_branches" ]; then
echo "$remote_branches"
fi
if [ -n "$local_branches" ]; then
echo "$local_branches"
fi
/bin/echo -n "Continue? (y/N): "
read choice
case $choice in
y|Y)
# Remove remote branches
#git push origin `git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n'`
# Remove local branches
git branch -d `git branch --merged | grep -v 'master$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n'`
;;
*)
echo "No branches removed."
;;
esac
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment