How to easily delete Git branches

· rrees's blog


Normally I have a hoarding problem with branches that have been merged but this one-liner makes it easy to tidy up commits that are following any kind of naming convention.

1git branch --list | grep <branch expression> | xargs git branch -d

Encapsulating this into a utility shell script...

 1#!/bin/bash
 2
 3if [ $# -lt 1 ] || [ $# -gt 2 ]
 4then
 5    echo "Usage: $0 <branch prefix> [force flag]"
 6    exit 1
 7fi
 8
 9PREFIX=$1
10FORCE=$2
11
12if [ "$FORCE" == "force" ]
13then
14    git branch --list | grep "$PREFIX" | xargs git branch -D
15else
16    git branch --list | grep "$PREFIX" | xargs git branch -d
17fi