#!/usr/bin/env bash # Removes old Git branches and does other cleanup # # Usage: # git-cleanup [--force] # # See explanation here: http://erikaybar.name/git-deleting-old-local-branches/ # # Author: Artem Sapegin, sapegin.me # License: MIT # https://github.com/sapegin/dotfiles # Exit on any failed command set -e CYAN="$(tput setaf 6)" UNDERLINE="$(tput sgr 0 1)" NOCOLOR="$(tput sgr0)" function header() { echo -e "\n$UNDERLINE$CYAN$1$NOCOLOR\n"; } function branches() { git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' } if [[ "$1" != "--force" ]]; then branches exit 1 fi # Delete all unreachable objects from the object database header "Deleting unreachable objects..." git prune # Delete all stale remote-tracking branches, these branches have already been # removed from the remote repository, but are still locally available in "remotes/". header "Deleting stale remote-tracking branches..." git remote prune origin echo "Done." # Delete branches for which remote branches were removed header "Deleting branches with no longer existing remote branches..." branches | xargs git branch -D