Last active
June 15, 2021 21:01
-
-
Save chernjie/e12a1233e83011a5c17451e2a42d5b8e to your computer and use it in GitHub Desktop.
Find redundant branches and print commands to delete them
This file contains 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
#!/usr/bin/env bash | |
COLOR_YELLOW(){ echo -en "\033[33m"; } | |
COLOR_RESET() { echo -en "\033[0m"; } | |
MAIN_BRANCH=${1:-master} | |
MAIN_REMOTE=${2:-origin} | |
label() { | |
echo -e "\033[33m$@\033[0m" >&2 | |
} | |
label remote branch cleanup | |
# git remote prune --dry-run $MAIN_REMOTE | |
git branch -r | | |
grep $MAIN_REMOTE/ | | |
grep \ | |
-ve $MAIN_REMOTE/$MAIN_BRANCH \ | |
-ve $MAIN_REMOTE/pull \ | |
-ve $MAIN_REMOTE/master \ | |
-ve $MAIN_REMOTE/release \ | |
-ve HEAD | | |
while read i | |
do | |
git branch --contains $i | | |
grep ^\*\ $MAIN_BRANCH$ -q && | |
echo $i | | |
cut -d/ -f2- | | |
sed -e s/^/:/ | |
done | | |
xargs echo git push $MAIN_REMOTE | |
label local branch cleanup | |
git branch | | |
grep \ | |
-ve $MAIN_BRANCH \ | |
-ve '^\+ ' \ | |
-ve '^\* ' \ | |
-ve master \ | |
-ve release \ | |
-ve HEAD | | |
while read i | |
do | |
git branch --contains "$i" | | |
grep ^\*\ $MAIN_BRANCH$ -q && | |
echo $i | |
done | | |
xargs echo git branch -d |
Author
chernjie
commented
Nov 1, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment