Clean Gone Branches
Execute the following to clean up stale local branches deleted from the remote.
Steps
- •
List branches to identify [gone] status
bashgit branch -v
Note: Branches with '+' prefix have worktrees that must be removed first.
- •
Identify worktrees for [gone] branches
bashgit worktree list
- •
Remove worktrees and delete [gone] branches
bashgit branch -v | grep '\[gone\]' | sed 's/^[+* ]//' | awk '{print $1}' | while read branch; do echo "Processing branch: $branch" 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 echo " Deleting branch: $branch" git branch -D "$branch" done
Expected Outcome
- •List all local branches with status
- •Remove worktrees for [gone] branches
- •Delete branches marked as [gone]
- •Report which worktrees and branches were removed
If no branches are marked as [gone], report that no cleanup was needed.