Skip to content

Instantly share code, notes, and snippets.

@harry0805
Created December 30, 2025 04:25
Show Gist options
  • Select an option

  • Save harry0805/26b2ad6c358dce3bbecefc9314410784 to your computer and use it in GitHub Desktop.

Select an option

Save harry0805/26b2ad6c358dce3bbecefc9314410784 to your computer and use it in GitHub Desktop.

Git alias for deleting merged local branches

Setup

Add this to your .gitconfig file.

[alias]
	cleanup = !git branch --merged | grep -v \"\\*\" | grep -v \"\\+\" | grep -v main | grep -v master | grep -v dev | xargs -r -p git branch -D

Usage

Run git cleanup, after the prompt, enter y to confirm, and it will automatically delete all the merged branches in your repository.

This will not delete the following branches:

  • main
  • master
  • dev
  • Current HEAD
  • Branches checked out in a worktree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment