# How to delete all merged branches?

2 messages from 2014-02-16 to 2014-02-16. Participants: Stefan Beller, Jamie Couture.
Thread: https://gitlist.dev/t/35892

## Stefan Beller, 2014-02-16 17:37

Subject: How to delete all merged branches?
Message-ID: <5300F748.2060805@googlemail.com>
URL: https://gitlist.dev/e/5300F748.2060805%40googlemail.com

```
Hi,

so I tend to accumulate lots of branches as I'd do one 
branch per feature. When cleaning up, I'd like to 
delete all branches, which have been merged.
I could use 
	$ git branch -d (which was merged already?) ^C
	$ git branch --merged # let's find out
	...
	$ # Now run git branch -d with each of the branches.

This kind of question has already been discussed, 
http://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-are-already-merged
suggests:
	git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

Now it seems as if this operation would be needed by quite a few people
actually. Maybe it's time to integrate into git? I'd be interested, which
way would be the most git-like way to do it.
I could think of:
	$ git branch -d --merged # no need to specifiy a branch iff --merged is given with -d
	$ git branch --delete-merged 	# comes as an new extra option, doesn't clutter other commands
	$ git branch -d keyword-for-all-merged-branches

Before starting such a feature, I'd like to hear input of others.

Thanks,
Stefan

```

## Jamie Couture, 2014-02-16 19:17

Subject: Re: How to delete all merged branches?
Message-ID: <20140216191721.GA29471@neptune>
URL: https://gitlist.dev/e/20140216191721.GA29471%40neptune
In-Reply-To: <5300F748.2060805@googlemail.com>

```
On Sun, Feb 16, 2014 at 06:37:12PM +0100, Stefan Beller wrote:
> Hi,
> 
> so I tend to accumulate lots of branches as I'd do one 
> branch per feature. When cleaning up, I'd like to 
> delete all branches, which have been merged.
> I could use 
> 	$ git branch -d (which was merged already?) ^C
> 	$ git branch --merged # let's find out
> 	...
> 	$ # Now run git branch -d with each of the branches.
> 
> This kind of question has already been discussed, 
> http://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-are-already-merged
> suggests:
> 	git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

probably saner to check where you are first, and have it conform to
some branch naming scheme; maybe someting like:

 - check you're on master
 - only consider branches that begin with first-last-/

-- 8< --
head=$(git rev-parse --verify HEAD)
master=$(git rev-parse --verify master)

if test "$head" != "$master"; then
    echo "You must be on master"
    exit 1
fi

git branch --merged  |
sed -n -e 's/\ *//' -e '/^[^/][^/]\//p' |
xargs git branch -d
-- 8< --

> 
> Now it seems as if this operation would be needed by quite a few people
> actually. Maybe it's time to integrate into git? I'd be interested, which
> way would be the most git-like way to do it.
> I could think of:
> 	$ git branch -d --merged # no need to specifiy a branch iff --merged is given with -d
> 	$ git branch --delete-merged 	# comes as an new extra option, doesn't clutter other commands
> 	$ git branch -d keyword-for-all-merged-branches
> 
> Before starting such a feature, I'd like to hear input of others.

```
