Skip to content

Instantly share code, notes, and snippets.

@chernjie
Last active June 15, 2021 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernjie/e12a1233e83011a5c17451e2a42d5b8e to your computer and use it in GitHub Desktop.
Save chernjie/e12a1233e83011a5c17451e2a42d5b8e to your computer and use it in GitHub Desktop.
Find redundant branches and print commands to delete them
#!/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
@chernjie
Copy link
Author

chernjie commented Nov 1, 2016

USAGE
-----

    branch-cleanup.sh <main_branch>

    Find redundant branches and print commands to delete them
    `main_branch` (Optional) defaults to "master"

EXAMPLE
-------

    branch-cleanup.sh | bash

    This command will delete redundant branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment