Last active
June 30, 2016 13:33
-
-
Save nickvergessen/b9553a93870aebea88115fa9c127a71d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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