Created
February 15, 2025 08:23
-
-
Save socketbox/9edaaeab672601973d570008565eaa53 to your computer and use it in GitHub Desktop.
Remove a git worktree and associated 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
#!/usr/bin/env zsh | |
# git worktree remove error codes | |
# 128: not a working tree OR cannot remove worktree with submodule | |
# 1: | |
# git branch -d error codes | |
# 1: error- branch foo not found | |
# 1: error- branch not fully merged | |
# we don't set -e here because we want to continue on error | |
set -uo pipefail | |
if [ -z $1 ] | |
then | |
echo "Pass in the name of the worktree" | |
fi | |
wt_name=$1 | |
echo "Fixing ownership..." | |
sudo chown chb:adm -R "./$1" | |
echo "Removing worktree..." | |
git worktree remove $wt_name | |
wtr_exit=$? | |
if [ "$wtr_exit" -eq 255 ] | |
then | |
echo "Going sudo/chown route..." | |
sudo chown -R chb: $wt_name | |
git worktree remove $wt_name | |
fi | |
if [ "$wtr_exit" -eq 128 ] | |
then | |
echo "Exit code: $wtr_exit" | |
echo "Contemplating the forced removal of worktree..." | |
#don't actually forcibly remove the worktree, but provide the command to do so | |
echo "#git worktree remove --force $wt_name" | |
elif [ "$wtr_exit" -ne 0 ] | |
then | |
echo "Exit code: $wtr_exit" | |
echo "Non-zero exit status when attempting to remove worktree. Exiting" | |
exit | |
else | |
git branch -d $wt_name | |
gbd_exit=$? | |
if [ $gbd_exit -ne 0 ] | |
then | |
echo "Exit code: $gbd_exit" | |
echo "Non-zero exit status when attempting to delete branch; using -D flag." | |
git branch -D $wt_name | |
exit | |
fi | |
fi | |
echo "Remember to run 'git brd $1' and 'git bD $1' to remove local and remote branches" | |
exit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment