Skip to content

Instantly share code, notes, and snippets.

@shaunmolloy
Last active October 26, 2022 13:45
Show Gist options
  • Save shaunmolloy/3881f5f189ba724475771b21eb597b99 to your computer and use it in GitHub Desktop.
Save shaunmolloy/3881f5f189ba724475771b21eb597b99 to your computer and use it in GitHub Desktop.
git-merged - Delete local branches that have been merged in
#!/usr/bin/env bash
# git-merged
# Delete local branches that have been merged in
# Compare against current branch
branch="$(git branch --show-current)"
# Compare to branch, when merged in
head="$(git reflog show HEAD -1 | tail -n1 | awk '{ print $1 }')"
branches="$(git branch --merged $branch | \
grep -Ev "(^\*|main|master|develop|dev|production|staging|release)" | \
perl -p -w -e 's/^\s+//g')"
IFS=$'\n' aBranches=(${branches})
branchCount=${#aBranches[*]}
if [ "$branchCount" == 0 ]; then
echo -e "Nothing to remove.\n"
else
bold=$(tput bold)
normal=$(tput sgr0)
# find merged branches
echo "${bold}Merged branches:${normal}"
echo "${branches}"
# confirm delete
echo && read -p "${bold}Delete branches?${normal} Y/y " -n 1 -r && echo
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
for branch in ${aBranches[@]}; do
git branch -d "${branch}"
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment