Script to clean up branches#1218
Conversation
There was a problem hiding this comment.
main, but PRs should target staged.
The main branch is auto-published from staged and should not receive direct PRs.
Please close this PR and re-open it against the staged branch.
You can change the base branch using the Edit button at the top of this PR,
or run: gh pr edit 1218 --base staged
There was a problem hiding this comment.
Pull request overview
Adds a repository utility script to help developers clean up local git branches whose upstream remotes have been pruned (marked as [gone]).
Changes:
- Introduces
scripts/delete-gone-branches.shto detect local branches with gone upstreams. - Supports dry-run by default and destructive deletion via
--apply. - Skips deleting the currently checked-out branch.
| git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads | | ||
| while IFS= read -r line; do | ||
| branch=${line% *} | ||
| tracking=${line#"$branch "} |
There was a problem hiding this comment.
The for-each-ref output is parsed by splitting on the last space (branch=${line% *}), which is brittle and can mis-parse if the formatted fields ever contain spaces (or if the format is adjusted later). Prefer using an explicit delimiter in the --format (e.g., a tab) and read the two fields separately to make the selection of [gone] branches robust.
| git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads | | |
| while IFS= read -r line; do | |
| branch=${line% *} | |
| tracking=${line#"$branch "} | |
| git for-each-ref --format=$'%(refname:short)\t%(upstream:track)' refs/heads | | |
| while IFS=$'\t' read -r branch tracking; do |
| git branch -D "$branch" | ||
| deleted_count=$((deleted_count + 1)) |
There was a problem hiding this comment.
Because the script runs with set -e, any failure from git branch -D (e.g., branch checked out in another worktree, permissions, etc.) will terminate the whole script and skip deleting the remaining branches. Consider handling deletion per-branch (capturing failures and continuing) and only incrementing deleted_count on successful deletes, so one problematic branch doesn’t stop the cleanup run.
| git branch -D "$branch" | |
| deleted_count=$((deleted_count + 1)) | |
| if git branch -D "$branch"; then | |
| deleted_count=$((deleted_count + 1)) | |
| else | |
| echo "Failed to delete branch: $branch" >&2 | |
| fi |
No description provided.