git: Delete All Branches but Master

By  on  

Maintenance is incredibly important in any project, but if you want to take your professionalism to the next level, you should keep your git environment in shape.  Unfortunately I'm not that guy -- I leave git branches laying around, even after they've been merged into master.  GitHub even provides a button to do the cleanup but I can't be bothered.  Not good.

When you're ready to do some real cleanup on a repository, throw this at it:

git branch | grep -v "master" | sed 's/^[ *]*//' | sed 's/^/git branch -d /' | bash

The shell command above deletes every branch in your local checkout except for master branch.  This is a dangerous script but you could always check out a given branch from a remote like GitHub if you happen to need it!

Recent Features

  • By
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    pointer Media Query

    As more devices emerge and differences in device interaction are implemented, the more important good CSS code will become.  In order to write good CSS, we need some indicator about device capabilities.  We've used CSS media queries thus far, with checks for max-width and pixel ratios.

  • By
    Create a Simple Slideshow Using MooTools, Part II:  Controls and Events

    Last week we created a very simple MooTools slideshow script. The script was very primitive: no events and no next/previous controls -- just cross-fading between images. This tutorial will take the previous slideshow script a step further by: Adding "Next" and "Previous" controls. Adding...

Discussion

  1. The script becomes less dangerous, when it uses the

    git branch -d

    variant instead of the upper case -D. Then branches are only deleted, when they are already merged in any of the remaining branches, and no work is lost. (Also, the error messages show you, which branches have work, that has not yet landed in master.)

  2. git remote prune origin -)

  3. lev
    git branch | grep -v "master" | xargs git branch -d
  4. Allie F

    Thank you, this worked well for me. I first tried it with -d, which deleted some, and then decided to go for -D, which did end up deleting everything but master.

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