Skip to content

Instantly share code, notes, and snippets.

@speto
Forked from zasadnyy/git-deletebranches.sh
Last active August 14, 2017 12:47
Show Gist options
  • Save speto/cb5e5b87daeb9436db0d6bd13b69a0c8 to your computer and use it in GitHub Desktop.
Save speto/cb5e5b87daeb9436db0d6bd13b69a0c8 to your computer and use it in GitHub Desktop.
Git Delete Merged Branches
#!/bin/bash
MAIN=master
BRANCHES=$(git branch --merged $MAIN | grep -v -e 'master\|staging\|develop\|\*')
echo Branches merged into $MAIN:
echo $BRANCHES | tr " " "\n"
read -p "Delete these branches (y/n)? " answer
if [ "$answer" = "n" ]; then echo aborting && exit; fi
echo $BRANCHES | xargs -n 1 git branch -d

Git Delete Merged Branches

This is a handy script to delete any leftover and merged git branches. It will detect branches merged into development and delete those, except branches named master or staging. These branch names can be configured by editing the script.

To install simply run:

curl https://git.io/v77l1 | bash

Or manually put this script into ~/bin and run chmod +x ~/bin/git-deletebranches.sh.

If you run it the output will look like this:

~/my-git-repo> git-deletebranches.sh
Branches merged into master:
some-merged-feature-branch
another-merged-feature-branch
Delete these branches (y/n)? y
Deleted branch some-merged-feature-branch (was 281d690).
Deleted branch another-merged-feature-branch (was 0b67c29).

Enjoy! :)

#!/bin/sh
mkdir -p ~/bin
curl https://gist.githubusercontent.com/speto/cb5e5b87daeb9436db0d6bd13b69a0c8/raw/27906b4dcec7cb242657c7cfb59b9be6d907a713/git-deletebranches.sh > ~/bin/git-deletebranches.sh
chmod +x ~/bin/git-deletebranches.sh
echo git-deletebranches.sh installed successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment