Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git command to clean up merged branches.

git clean-branches

Deletes local or remote branches, except for master and develop.

Install

Copy git-clean-branches to your ~/bin or wherever.

Usage

Delete all merged local branches (except master and develop)

$ git clean-branches

Delete all merged remote branches on origin (except master and develop)

$ git clean-branches -r

or

$ git clean-branches --remote

or

$ git clean-branches remote

Delete all merged branches on a different remote

$ git clean-branches -r other-remote
#!/bin/sh
if [[ $1 == -r ]] || [[ $1 == --remote ]] || [[ $1 == remote ]] ; then
REMOTE=origin
if [[ $2 != '' ]] ; then
REMOTE=$2
fi
git remote prune $REMOTE
git branch -r --merged | grep "^\s*$REMOTE" | grep -v "\*\|master\|develop" | sed -e "s#^\s*$REMOTE/##" | xargs -n 1 git push $REMOTE --delete
else
git branch --merged | grep -v "\*\|master\|develop" | xargs -n 1 git branch -d
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.