How I clean up my local git branches

Kevin McCarthy
1 min readSep 1, 2019

--

I am quite messy. In life and sometimes in my coding. This is not something I am proud of but thank goodness for linters and code formatters!

Everyone knows, every time you are done with a branch locally you should delete it. Of course this makes sense.

However….

Sometimes I don’t. A lot of the time I don’t.

So I made a tiny script which helps me keep my git branches clean.

It presents each branch to you and you can put ‘y’ or ‘n’.

# ~/clean_up.rb
git_branch_command = 'git branch'
branches = `#{git_branch_command}`
deleteable_branches = branches.split(' ').reject { |branch| branch == 'development' || branch == 'master' || branch == '*' }
branches_to_delete = ''
deleteable_branches.each do |branch|
puts "want to delete #{branch}?"
input = STDIN.gets.chomp
branches_to_delete += "#{branch} " if input == 'y'
end
command = "git branch -D #{branches_to_delete}"
puts "execute command '#{command}'?"
input = STDIN.gets.chomp
if input == 'y'
`#{command}`
else
puts 'no branches deleted'
end
# in your dotfile where your aliases live, could be .zshrc or .bashrc
alias clean_up="ruby ~/clean_up_branches.rb"

--

--

No responses yet