Delete Merged Branches with git

By  on  

It's common courtesy to keep your git branch list clean, especially when colleagues need to fetch your remote branches.  I'm a bit of a ... offender, when it comes to maintaining my git branch list.  My colleague John Karahalis is not, however, and he hooked me up with an awesome git alias for deleting branches that have been merged into master. Place the following within your .git/config file:

[alias]
  delete-merged-branches = "!f() { git checkout --quiet master && git branch --merged | grep --invert-match '\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f"

You can run this command via:

git delete-merged-branches

The script worked masterfully -- all branches merged into master were gone and I was left to evaluate which local branches were left to send pull requests for.  Beautiful.  Keep this around if you're a lazy git-ter like me!

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    HTML5 Datalist

    One of the most used JavaScript widgets over the past decade has been the text box autocomplete widget.  Every JavaScript framework has their own autocomplete widget and many of them have become quite advanced.  Much like the placeholder attribute's introduction to markup, a frequently used...

  • By
    Create a Sexy Persistent Header with Opacity Using MooTools or jQuery

    I've been working with the Magento eCommerce solution a lot lately and I've taken a liking to a technique they use with the top bar within their administrative control panel. When the user scrolls below a specified threshold, the top bar becomes attached to the...

Discussion

  1. Nice tip – thanks

  2. Stophe

    Thanks, very handy. Also found this https://gist.github.com/malclocke/943565 to do the same for all merged remote branches.

  3. Tim Smith

    Also note that you can prune remote-tracking references (e.g., remotes/foo/patch-1) with either fetch --prune foo or remote prune foo.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!