Skip to content

Instantly share code, notes, and snippets.

@meriadec
Last active February 23, 2017 05:50
Show Gist options
  • Save meriadec/9e91a8fdba5cc14f53dbd66be66c427e to your computer and use it in GitHub Desktop.
Save meriadec/9e91a8fdba5cc14f53dbd66be66c427e to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Usage:
# ./remove-merged <branch>
#
# if no branch given, use the current branch
#
targetBranch=$1
# if no target branch given, assume it is the current branch
if [ "$targetBranch" == "" ]; then
targetBranch=$(git branch | grep \* | sed -E 's/\* (.*)$/\1/g')
fi
# get branches that needs to be deleted
branchesToDelete=$(git branch -r --merged $targetBranch \
| grep -v $targetBranch \
| grep -v '/HEAD')
for b in $branchesToDelete; do
remote=$(echo $b | sed -E 's/([^/]*).*/\1/g')
bName=$(echo $b | sed -E 's/[^/]*\/(.*)/\1/g')
# echo command instead of executing it :D
# execute at your risks :D
#
# git push $remote :$bName
echo "git push $remote :$bName"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment