Removing git branches at once

How to remove several branches at once, locally and remotly.

June 3, 2016 - 1 minute read -
git branch

Sometimes, we realized that we have many branches that we don’t need anymore. It can be a tedious task delete them one by one.

In order to avoid that, we can automate this task. Using this script, you just need to specify which branches you want to preserve, run it and that is all. Your ugly/unfriendly branches should been gone.

#!/bin/sh
# branch list to not delete
branch_not_delete=( "master" "develop" "our-branch-1" "our-branch-2")


# Iterate over remotes branch and if they aren't in our previous list, we deleted # them
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do

	# delete prefix remotes/origin/ from branch name
	branch_name="$(awk '{gsub("remotes/origin/", "");print}' <<< $branch)"
	
	if ! [[ " ${branch_not_delete[*]} " == *" $branch_name "* ]]; then
		# delete branch remotly and locally
    	git push origin :$branch_name
	fi
done