Skip to content

Instantly share code, notes, and snippets.

@AlexFBP
Created January 22, 2023 19:04
Show Gist options
  • Save AlexFBP/9d5ffcc464109c141e5c698772264f77 to your computer and use it in GitHub Desktop.
Save AlexFBP/9d5ffcc464109c141e5c698772264f77 to your computer and use it in GitHub Desktop.
Clean local branches that have been deleted in remote

TLDR

Add this alias (enter this once on your terminal)

git config --global alias.cb '!git fetch -p && for branch in $(git for-each-ref --format '\''%(refname) %(upstream:track)'\'' refs/heads | awk '\''$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'\''); do git branch -D $branch; done'

so whenever you need to clean your local branches without removing just need to

git cb

Reference / Detail

This is an improvement of: https://stackoverflow.com/a/33548037/3180052

Please note that this will work best (and most of the git workflow) if you keep commands simple (KISS) and if you always read the git console output after each command

Avoid adding anything else to git push or git pull unless the same git output tells you to do so!

(Just use git push, not git push origin ... and so on, keep it simple, please! - add only words/parameters you are trully sure of its purpose)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment