Created
March 2, 2021 12:27
-
-
Save gaggle/168d7b5c8e9430f237628fa19e108b71 to your computer and use it in GitHub Desktop.
Script for pruning Git branches (designed to work on MacOS 11 and newer). Works with base branch "master" or "main" and you choose whether to prune remote AND local branches, or only local 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
alias git-remove-merged="git-remove-merged.sh" |
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
#!/usr/bin/env zsh | |
autoload colors | |
colors | |
function remove_remote_branches() { | |
# Remove remote branches | |
git push origin $(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$" | sed 's/origin\//:/g' | tr -d '\n') | |
} | |
function remove_local_branches() { | |
# Remove local branches | |
git branch -d $(git branch --merged | grep -v 'master$' | grep -v "$current_branch$" | sed 's/origin\///g' | tr -d '\n') | |
} | |
# Script to delete remote and local merged branches | |
current_branch=$(git branch --no-color 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
if [ "$current_branch" != "master" ] && [ "$current_branch" != "main" ]; then | |
echo "${fg_bold[red]}WARNING${reset_color}: You are on branch ${fg_bold[default]}$current_branch${reset_color}, ${fg_bold[red]}NOT${reset_color} master or main." | |
echo "No branches removed." | |
else | |
echo "Fetching merged branches..." | |
git remote prune origin | |
remote_branches=$(git branch -r --merged | grep -v '/master$' | grep -v "/$current_branch$") | |
local_branches=$(git branch --merged | grep -v 'master$' | grep -v "$current_branch$") | |
if [ -z "$remote_branches" ] && [ -z "$local_branches" ]; then | |
echo "No existing branches have been merged into ${fg_bold[default]}$current_branch${reset_color} 🎉" | |
else | |
if [ -n "$remote_branches" ]; then | |
echo "${fg_bold[default]}Remote${reset_color} branches that can be removed:" | |
echo "$remote_branches" | |
fi | |
if [ -n "$local_branches" ]; then | |
echo "${fg_bold[default]}Local${reset_color} branches that can be removed:" | |
echo "$local_branches" | |
fi | |
PS3='Choose which branches to remove: ' | |
choices=("All" "Local only" "Exit") | |
select ans in "${choices[@]}"; do | |
case $ans in | |
"All") | |
if [ -n "$remote_branches" ]; then | |
remove_remote_branches | |
fi | |
if [ -n "$local_branches" ]; then | |
remove_local_branches | |
fi | |
break | |
;; | |
"Local only") | |
if [ -n "$local_branches" ]; then | |
remove_local_branches | |
fi | |
break | |
;; | |
"Exit") | |
echo "No branches removed" | |
exit | |
;; | |
*) echo "invalid option $REPLY" ;; | |
esac | |
done | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment