Skip to content

Instantly share code, notes, and snippets.

@eponvert
Last active June 30, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eponvert/28c0f3a9743132032761eed7203dc48c to your computer and use it in GitHub Desktop.
Save eponvert/28c0f3a9743132032761eed7203dc48c to your computer and use it in GitHub Desktop.
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