Created
February 20, 2026 17:49
-
-
Save andrewaylett/27c6a33bd2fc8c99eada60589f0ca31f to your computer and use it in GitHub Desktop.
A git custom command to clean up unneeded local branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| for branch in $(git branch --format="%(refname:lstrip=2)" --merged) | |
| do | |
| echo "Considering $branch" | |
| if git config branch.$branch.remote | |
| then | |
| # Branch has remote tracking | |
| ORIGIN_NAME=$(git config branch.$branch.remote) | |
| ORIGIN_REF=$(git config branch.$branch.merge) | |
| if git ls-remote --exit-code $ORIGIN_NAME $ORIGIN_REF | |
| then | |
| echo "Remote branch still exists, skipping" | |
| else | |
| echo "Removing branch" | |
| git branch -D $branch | |
| fi | |
| else | |
| echo "Tracking not presnt, assuming not yet pushed" | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment