Skip to content

Instantly share code, notes, and snippets.

@chreke
Last active August 29, 2015 14:07
Show Gist options
  • Save chreke/b093c576e5bdcb0796cb to your computer and use it in GitHub Desktop.
Save chreke/b093c576e5bdcb0796cb to your computer and use it in GitHub Desktop.
Ruby script to clean up dead git branches
#!/usr/bin/ruby
# Interactively remove branches not in remote, *by name*. This is
# useful for pruning branches that were rebased prior to merging,
# since those branches will not show up in `git branch --merged`.
# List local branches which may have been merged
remotes = `git branch -r`.split.map {|x| x.sub('origin/', '') }
locals = `git branch`.split.select {|x| x != '*' }
complement = locals.select {|x| not (remotes.include? x) }
# Ask if local branches should be deleted
for branch in complement do
puts "'#{branch}' not in remotes, delete?"
if gets.chomp == "y"
`git branch -D #{branch}` and puts "Deleted #{branch}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment