Skip to content

Instantly share code, notes, and snippets.

@jsonberry
Last active August 8, 2022 14:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsonberry/426766b9cf732e9aad98af3eab3de90c to your computer and use it in GitHub Desktop.
Save jsonberry/426766b9cf732e9aad98af3eab3de90c 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