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
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    CSS Triangles

    I was recently redesigning my website and wanted to create tooltips.  Making that was easy but I also wanted my tooltips to feature the a triangular pointer.  I'm a disaster when it comes to images and the prospect of needing to make an image for...

  • By
    Elegant Overflow with CSS Ellipsis

    Overflow with text is always a big issue, especially in a programmatic environment. There's always only so much space but variable content to add into that space. I was recently working on a table for displaying user information and noticed that longer strings were...

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!