Clean up local git branches that have been deleted from remote
Removes local branches that were deleted from remote, including their worktrees. Use after `git fetch --prune` to clean up stale branches marked as [gone].
/plugin marketplace add br-huang/claude-toolkit/plugin install commit-workflow@Dev-toolkitClean up stale local branches that have been deleted from the remote repository (marked as [gone]).
List branches to identify [gone] status
git branch -v
Note: Branches with a '+' prefix have associated worktrees and must have their worktrees removed before deletion.
Identify worktrees that need removal
git worktree list
Remove worktrees and delete [gone] branches
# Process all [gone] branches, removing '+' prefix if present
git branch -v | grep '\[gone\]' | sed 's/^[+* ]//' | awk '{print $1}' | while read branch; do
echo "Processing branch: $branch"
# Find and remove worktree if it exists
worktree=$(git worktree list | grep "\\[$branch\\]" | awk '{print $1}')
if [ ! -z "$worktree" ] && [ "$worktree" != "$(git rev-parse --show-toplevel)" ]; then
echo " Removing worktree: $worktree"
git worktree remove --force "$worktree"
fi
# Delete the branch
echo " Deleting branch: $branch"
git branch -D "$branch"
done
After executing these commands:
If no branches are marked as [gone], report that no cleanup was needed.
Run git fetch --prune first to update remote tracking information if branches show [gone] status incorrectly.