-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Script to clean up branches #1218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||||||||
| #!/bin/bash | ||||||||||||||||
|
|
||||||||||||||||
| set -euo pipefail | ||||||||||||||||
|
|
||||||||||||||||
| usage() { | ||||||||||||||||
| cat <<'EOF' | ||||||||||||||||
| Usage: bash scripts/delete-gone-branches.sh [--apply] | ||||||||||||||||
|
|
||||||||||||||||
| Find local branches whose upstream is marked "[gone]" and delete them. | ||||||||||||||||
|
|
||||||||||||||||
| Options: | ||||||||||||||||
| --apply Actually delete the branches with `git branch -D` | ||||||||||||||||
| --help Show this help text | ||||||||||||||||
|
|
||||||||||||||||
| Without --apply, the script prints what it would delete. | ||||||||||||||||
| EOF | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| apply=false | ||||||||||||||||
|
|
||||||||||||||||
| case "${1:-}" in | ||||||||||||||||
| "") | ||||||||||||||||
| ;; | ||||||||||||||||
| --apply) | ||||||||||||||||
| apply=true | ||||||||||||||||
| ;; | ||||||||||||||||
| --help|-h) | ||||||||||||||||
| usage | ||||||||||||||||
| exit 0 | ||||||||||||||||
| ;; | ||||||||||||||||
| *) | ||||||||||||||||
| echo "Unknown option: $1" >&2 | ||||||||||||||||
| usage >&2 | ||||||||||||||||
| exit 1 | ||||||||||||||||
| ;; | ||||||||||||||||
| esac | ||||||||||||||||
|
|
||||||||||||||||
| git fetch --prune --quiet | ||||||||||||||||
|
|
||||||||||||||||
| mapfile -t gone_branches < <( | ||||||||||||||||
| git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads | | ||||||||||||||||
| while IFS= read -r line; do | ||||||||||||||||
| branch=${line% *} | ||||||||||||||||
| tracking=${line#"$branch "} | ||||||||||||||||
| if [[ "$tracking" == "[gone]" ]]; then | ||||||||||||||||
| printf '%s\n' "$branch" | ||||||||||||||||
| fi | ||||||||||||||||
| done | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| if [[ ${#gone_branches[@]} -eq 0 ]]; then | ||||||||||||||||
| echo "No local branches with gone upstreams found." | ||||||||||||||||
| exit 0 | ||||||||||||||||
| fi | ||||||||||||||||
|
|
||||||||||||||||
| current_branch="$(git branch --show-current)" | ||||||||||||||||
|
|
||||||||||||||||
| echo "Found ${#gone_branches[@]} branch(es) with gone upstreams:" | ||||||||||||||||
| printf ' %s\n' "${gone_branches[@]}" | ||||||||||||||||
|
|
||||||||||||||||
| if [[ "$apply" != true ]]; then | ||||||||||||||||
| echo | ||||||||||||||||
| echo "Dry run only. Re-run with --apply to delete them." | ||||||||||||||||
| exit 0 | ||||||||||||||||
| fi | ||||||||||||||||
|
|
||||||||||||||||
| deleted_count=0 | ||||||||||||||||
|
|
||||||||||||||||
| for branch in "${gone_branches[@]}"; do | ||||||||||||||||
| if [[ "$branch" == "$current_branch" ]]; then | ||||||||||||||||
| echo "Skipping current branch: $branch" | ||||||||||||||||
| continue | ||||||||||||||||
| fi | ||||||||||||||||
|
|
||||||||||||||||
| git branch -D "$branch" | ||||||||||||||||
| deleted_count=$((deleted_count + 1)) | ||||||||||||||||
|
Comment on lines
+75
to
+76
|
||||||||||||||||
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
for-each-refoutput 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.