March 2, 2024
git fetch --prune;
pruned_branches=$(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads refs/remotes/origin | grep gone | cut -d " " -f 1 | sed 's/refs\/heads\///');
for branch in $pruned_branches; do
git branch -d $branch;
done
or for you one liner maschocists:
git fetch --prune; git for-each-ref --format '%(refname) %(upstream:track)' refs/heads refs/remotes/origin | grep gone | cut -d " " -f 1 | sed 's/refs\/heads\///' | xargs git branch -d;
git fetch --prune
- Fetches the latest changes from the remote and prunes (deletes) any remote-tracking references that no longer exist on the remote.git for-each-ref --format '%(refname) %(upstream:track)' refs/heads refs/remotes/origin
- Lists all local and remote branches and their tracking status.grep gone
- Filters out the branches that no longer exist on the remote.cut -d " " -f 1
- Extracts the branch name from the output.sed 's/refs\/heads\///'
- Removes therefs/heads/
prefix from the branch name.xargs git branch -d
- Deletes each pruned branch.
🙇
~