Skip to content

Instantly share code, notes, and snippets.

@darrenclark
Last active September 2, 2016 15:27
Embed
What would you like to do?
Git BRanchClean - Remove local branches that have been merged
#!/bin/bash
set -eu -o pipefail
## Git BRanchClean (removes local branches that have been merged into the current branch)
# List of branches to ignore separated by vertical bars/pipes (|)
IGNORED_BRANCHES="develop|master"
branches="$(git branch --no-column --no-color --merged | egrep -v '\* ' | sed 's/^[\* ] //' | (egrep -v "$IGNORED_BRANCHES" || true))"
if [[ -z "$branches" ]]; then
echo "No branches to delete"
exit
fi
echo "Following branches have been merged:"
for branch in $branches; do
echo "- $branch"
done
echo -n "Delete? [yN] "
read should_delete
if [[ "$should_delete" == "y" ]]; then
for branch in $branches; do
git branch -d "$branch"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment