Skip to content

Instantly share code, notes, and snippets.

@gambitier
Created October 15, 2025 06:38
Show Gist options
  • Select an option

  • Save gambitier/ad141dc022c8e31358f426214e5e9bdf to your computer and use it in GitHub Desktop.

Select an option

Save gambitier/ad141dc022c8e31358f426214e5e9bdf to your computer and use it in GitHub Desktop.

🧹 Git Alias: Clean All Local Branches and Prune Remotes

This alias lets you quickly delete all local branches except the current one and prune remote-tracking branches that no longer exist on the remote.


🚀 Setup

Run this command in your terminal to create the alias:

git config --global alias.clean-branches '!git branch | grep -v "^\*" | xargs git branch -D && git fetch --prune'

✅ Usage

Once added, you can run:

git clean-branches

This will:

  • Delete all local branches except the one you’re on.
  • Prune remote-tracking branches that were deleted on the remote.

🧠 How It Works

Part Description
git branch Lists all local branches
grep -v "^\*" Excludes the currently checked-out branch
xargs git branch -D Deletes each remaining branch forcefully
git fetch --prune Removes references to deleted remote branches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment