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
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

Incredible Demos

  • By
    Fade Images with MooTools LazyLoad

    I recently received an email from a MooTools developer asking a great question about my LazyLoad class: "I'm using your LazyLoad MooTools plugin (which is great, by the way). I have been trying to figure out how to modify it so that once an image scrolls into...

  • By
    CSS Scoped Styles

    There are plenty of awesome new attributes we've gotten during the HTML5 revolution:  placeholder, download, hidden, and more.  Each of these attributes provides us a different level of control over an element on the page, but there's a new element attribute that allows...

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!