Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajaddp/f9704465b0cb0a2c09f5fe53652ec8a4 to your computer and use it in GitHub Desktop.
Save sajaddp/f9704465b0cb0a2c09f5fe53652ec8a4 to your computer and use it in GitHub Desktop.
Git: Remove all local branches that are not on remote

Git: Remove all local branches that are not on remote

Method 1:

git fetch -p && for branch in $(git branch -vv | grep ': gone]' | awk '{print $1}'); do git branch -D $branch; done

Method 2 (safer):

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

Source

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