Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Last active July 28, 2021 19:57
Show Gist options
  • Save adamzaninovich/1f16ff481ac1285c9efb4181adea75b2 to your computer and use it in GitHub Desktop.
Save adamzaninovich/1f16ff481ac1285c9efb4181adea75b2 to your computer and use it in GitHub Desktop.
Git branch cleanup

Notes

This is not a script. You will want to just add these functions to your bash config (or make it into a script, do what you want, I'm not your mom)

You can remove all the other functions if you want to just replace ohai and error with echo for simplicity (I just have those formatting functions at the top of my alias/function file in my bash config and use them in a lot of my functions. They are originally from the homebrew sourcecode)

New Features

  • exit if not in git repo
  • support a main branch other than master (useful if you are using main or if you use something like gitflow and want to compare to develop, staging, etc
  • exit if master/main branch doesn’t exist
  • outputs number of cleaned branches
  • squeaky clean ✨

Screenshot

Screenshot

Todo

  • even if another main branch is specified, do not delete master or main
# string formatters
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_bold="$(tty_mkbold 39)"
tty_reset="$(tty_escape 0)"
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
ohai() {
printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
error() {
printf "${tty_red}ERROR:${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")"
}
# gg_cleanup:
# Accepts main comparison branch as the first arg
# Defaults to `master`
# Usage:
# gg_cleanup main
gg_cleanup () {
local main_branch before after cleaned_branches branches_left
if [[ "$(git status 2>&1)" =~ "not a git repository" ]]; then
error "Not in a git repository"
return 1
fi
if [[ -n "${1-}" ]]; then
main_branch="$1"
else
main_branch="master"
fi
branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
if [[ $branch != "$main_branch" ]]; then
if [[ "$(git checkout "$main_branch" 2>&1)" =~ "did not match" ]]; then
error "$main_branch does not exist. Please specify a main branch"
return 1
fi
echo ""
fi
before=$(git branch | wc -l)
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
after=$(git branch | wc -l)
cleaned_branches=$(( before - after ))
if [[ $cleaned_branches == 0 ]]; then
ohai "Nothing to clean."
else
echo ""
ohai "Cleaned up $cleaned_branches $(plural "branch" $cleaned_branches "es")."
fi
branches_left=$(( after - 1 ))
ohai "You have $branches_left $(plural "branch" $branches_left "es") left (other than $main_branch)."
[[ $branch != "$main_branch" ]] && [[ -n $(git branch | grep "$branch") ]] && echo "" && git checkout "$branch"
ohai "✨"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment