Skip to content

Instantly share code, notes, and snippets.

@xlymian
Created December 9, 2011 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlymian/1453368 to your computer and use it in GitHub Desktop.
Save xlymian/1453368 to your computer and use it in GitHub Desktop.
git-prunable-branches
#!/usr/bin/env ruby
# git_branch_prunable 0.1 - 2011-11-29
# by Paul Mylchreest (paul.mylchreest@mac.com)
#
%x( git branch ).each_line do |branch|
branch.strip!
next if branch =~ /master|development|staging/
cmd = "git log #{branch} -n 1 --pretty=oneline"
first_commit_line = %x( #{cmd} )
commit_id = first_commit_line.match( /([0-9a-z]){40,40}/ )[0]
cmd = "git branch --contains #{commit_id}"
branches_containing = %x( #{cmd} ).lines.map{|x| x.strip!}.
reject{|x| x =~ /#{branch}/}
if branches_containing.size > 0
puts "#{branch} is prunable!"
puts "branch: #{branch} commit => #{commit_id}"
puts "#{branches_containing}"
if %x( git branch -r | grep #{branch} ).lines.count == 0
puts "#{branch} has possibly no remote branch!"
puts "you may need to run: 'git branch -d #{branch}'"
end
puts "run: 'git push origin :#{branch}'"
puts "then: 'git branch -d #{branch}'"
puts '=' * 80
end
end
puts "run: 'git remote prune origin' when done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment