Skip to content

Instantly share code, notes, and snippets.

@rogeriopradoj
Forked from jsonberry/purge_branches.sh
Created August 8, 2022 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogeriopradoj/1737740a7e0293d70edf678bc38bd95f to your computer and use it in GitHub Desktop.
Save rogeriopradoj/1737740a7e0293d70edf678bc38bd95f to your computer and use it in GitHub Desktop.
Mass remove local git branches and prune remote tracking
// List all remotely tracked branches that do not exist in the remote repo
git remote prune origin --dry-run
// Prune all remotely tracked branches that do not exist in the remote repo
git remote prune origin
// List all local branches that were merged into master, explicitly exclude master from that list
git branch --merged master | grep -v 'master'
/*
Delete local branches
Each command does not delete master
*/
// Delete all local branches that were merged into master
git branch --merged master | grep -v 'master' | xargs git branch -d
// Safe deletion of all local branches (branches not merged into master will not get purged)
git branch | grep -v 'master' | xargs git branch -d
// Force delete of all local branches
git branch | grep -v 'master' | xargs git branch -D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment