A convenience wrapper around standard Git commands for pruning local branches whose upstreams have disappeared
xargs -p
.git branch -d
, which refuses to delete a branch that is not fully merged into the current HEAD.
In other words, even if the script’s “merged” test were wrong, Git itself will still protect you.Risk | How it could happen | Mitigation / recovery |
---|---|---|
Accidental loss of a useful local branch | You approve deletion, then realise later you still needed it (e.g., for an in-progress rebase). | The branch tip is still in git reflog for 90 days by default; you can git reflog , find the commit, and git branch <name> <SHA> . |
False “merged” classification | Very rare, but could occur if you’re on a branch other than the usual integration branch (main ) when you run the script, because git branch --merged compares against current HEAD. | Run the script from your primary integration branch, or change git branch --merged to --merged main . |
Remote renamed / forked | If a teammate renamed the remote branch and pushed it elsewhere, your local branch now shows “gone” even though work continues under a new name. Deleting it means you lose the convenient reflog link to its history. | Double-check the printed list before confirming; if in doubt, answer N and investigate. |
Edge-case branch names | Branch names containing newlines or unusual bytes could confuse text processing (though the script is largely null-delimited). | Unlikely in normal workflows; Git itself discourages such names. |
Script aborts mid-way | Because of set -euo pipefail , any unexpected error (e.g., network hiccup during git fetch ) terminates the script. Nothing is partially deleted. | Just re-run after fixing the root cause. |
main
or develop
— the worst-case consequence is deleting a branch you later decide you want, which is recoverable via git reflog
or by fetching it again if it still exists on a remote.