Removing git branches at once

- 1 min

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 
Levi Velázquez

Levi Velázquez

Full Stack Developer. Computer Engineer, Startup Enthusiast, CTO at Trulii.com more...

rss facebook twitter stackoverflow github youtube mail spotify instagram linkedin google pinterest medium