Created
April 23, 2023 18:10
-
-
Save crhistianramirez/b659dc3c9d7984136d7a8e521bf9a67c to your computer and use it in GitHub Desktop.
A bash function that enables you to mass delete REMOTE git branches
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 | |
# What is this? | |
# A bash function that enables you to mass delete REMOTE 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 git_remote (defaults to origin) as well as list of permanent_exclusions (defaults to development, main, and master) | |
# 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-remote="source ~/clean_branches_remote.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-remote="source ~/clean_branches_remote.sh; main" | |
# Running the command | |
# 1. Use the terminal to navigate to a github project | |
# 2. Run cb-remote | |
# Running the command with adhoc exclusions | |
# 1. Use the terminal to navigate to a github project | |
# 2. Run cb-remote followed by a comma separated string of branches to exclude for example: | |
# cb-remote branch1,branch2,branch3 | |
main () { | |
local git_remote=me # the name of the git remote that will be referenced for retrieval of branches | |
local permanent_exclusions_array=(development main master) # 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-remote 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 | |
# 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 -r | grep "${git_remote}/" | 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 Remote Branches 🗑️" | |
echo "$branches_to_delete" | |
echo -n "🚫 Are you sure you want to delete these REMOTE 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" | sed -e "s/\// :/" | xargs -n2 git push | |
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