Skip to content

Instantly share code, notes, and snippets.

@crhistianramirez
Created April 23, 2023 18:09
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 crhistianramirez/f3d9a58bb1ea7a80078b222d56699b2c to your computer and use it in GitHub Desktop.
Save crhistianramirez/f3d9a58bb1ea7a80078b222d56699b2c to your computer and use it in GitHub Desktop.
A bash function that enables you to mass delete LOCAL git branches
#!/bin/bash
# What is this?
# A bash function that enables you to mass delete LOCAL git branches
# When run, a prompt will appear showing the branches to delete and you can either
# choose to proceed or cancel
# Configuration
# You can provide a list of branches to always exclude as well as a list of branches
# to exclude at time of invocation (comma separated string). It is recommended to configure
# the the list of permanent_exclusions (defaults to none)
# Supported Shells
# The bash scripts have been tested against zsh for Mac and git bash for Windows
# other bash shells may also work, but have not been tested so your mileage may vary
# Recommended Installation for Mac (zsh)
# 1. Save the file in ~
# 2. Update your .zshrc file (~/.zshrc) with the following:
# alias cb-local="source ~/clean_branches_local.sh; main"
# Recommended Installation for Windows (git bash)
# 1. Save the file in ~
# 2. Update your .bash_profile file (~/.bash_profile) with the following:
# alias cb-local="source ~/clean_branches_local.sh; main"
# Running the command
# 1. Use the terminal to navigate to a github project
# 2. Run cb-local
# Running the command with adhoc exclusions
# 1. Use the terminal to navigate to a github project
# 2. Run cb-local followed by a comma separated string of branches to exclude for example:
# cb-local branch1,branch2,branch3
main () {
local permanent_exclusions_array=() # list of branches that should always be excluded from deletion
# combine permanent exclusions and adhoc exclusions
if [ $# -eq 0 ]; then
# user did not provide adhoc exclusions
local all_exclusions_array=("${permanent_exclusions_array[@]}")
else
local adhoc_exclusions="$1" # comma separated list of branches to exclude provided by user when the function is invoked. ie: cb-local branch1,branch2
local adhoc_exclusions_array=( $(echo "$adhoc_exclusions" | tr ',' '\n') ) # split adhoc_exclusions by , into array
local all_exclusions_array=( "${adhoc_exclusions_array[@]}" "${permanent_exclusions_array[@]}" )
fi
# exclude current branch from deletion as git will throw an error if we try to delete
local current_branch="$(git rev-parse --abbrev-ref HEAD)"
all_exclusions_array+=($current_branch)
# get list of branches to delete (minus any exclusions)
local exclusions_filter="$(echo "$(join_by_string "\\|" "${all_exclusions_array[@]}")")"
local branches_to_delete="$(git branch | grep -v "$exclusions_filter")"
# if no branches to delete, then exit early
if [ -z "$branches_to_delete" ]; then
echo "✨ Everything looks clean, no branches found ✨"
return
fi
echo "🗑️ Clean Local Branches 🗑️"
echo "$branches_to_delete"
echo -n "🚫 Are you sure you want to delete these LOCAL branches? [y/n] "
local old_stty_cfg="$(stty -g)"
stty raw -echo > /dev/null 2>&1
local answer="$( while ! head -c 1 | grep -i '[ny]' ;do true ;done )"
stty "$old_stty_cfg"
if echo "$answer" | grep -iq "^y" ;then
echo Yes
echo "$branches_to_delete" | xargs git branch -D
else
echo No
fi
}
# https://dev.to/meleu/how-to-join-array-elements-in-a-bash-script-303a
join_by_string() {
local separator="$1"
shift
local first="$1"
shift
printf "%s" "$first" "${@/#/$separator}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment