Skip to content

Instantly share code, notes, and snippets.

@nickvergessen
Last active June 30, 2016 13:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickvergessen/b9553a93870aebea88115fa9c127a71d to your computer and use it in GitHub Desktop.
Save nickvergessen/b9553a93870aebea88115fa9c127a71d to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Bash script to clean up stray branches that have no "upstream" branch
#
# USE AT OWN RISK
#
# gitclean [--no-interactive]
#
# - no-interactive: Automatically deletes the branch, without showing the diff
# and asking for confirmation
# Upstream name
UPSTREAM_REMOTE="origin"
set -e
NO_INTERACTIVE="0"
if [ "$1" = '--no-interactive' ]; then
NO_INTERACTIVE="1"
fi
BRANCHES=$(git branch --no-color | grep -v "^* " | grep -v "^master$" | grep -v "stable9")
for BRANCH in $BRANCHES
do
HAS_UPSTREAM="$(git branch -a --no-color | grep remotes/$UPSTREAM_REMOTE/$BRANCH | wc -l)"
if [ "$HAS_UPSTREAM" = "0" ]; then
if [ "$NO_INTERACTIVE" = "1" ]; then
git branch -D $BRANCH
continue
fi
echo "#"
echo "# Branch: $BRANCH"
echo "#"
echo ""
git --no-pager show $BRANCH
echo ""
echo "#"
echo "#"
read -p "# Branch $BRANCH has no $UPSTREAM_REMOTE branch, delete? [y/n] " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]; then
git branch -D $BRANCH
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment