Created
September 6, 2017 13:00
-
-
Save soberstadt/d7aa71e4dc9b9a9c682041fe2b9773d0 to your computer and use it in GitHub Desktop.
A git branch cleanup script for people who use 'Squash and merge' on Github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# There are other solutions to deleting local branches that have been deleted on the remote, | |
# but I have found they all assume you are deleting fully-merged branches on github, but | |
# because I use the 'Squash and merge' strategy, my branches are never actually merged. | |
# So if you use 'Squash and merge' on Github, this might be helpful for you! | |
deleted_branches = `git fetch -p --dry-run 2>&1 | grep deleted`.split("\n") | |
deleted_branches = deleted_branches.map do |line| | |
line.split('origin/')[1] | |
end | |
my_branch_statuses = `git for-each-ref --format="%(refname:short) %(upstream:track)" refs/heads`.split("\n") | |
branches_to_delete = [] | |
keep = [] | |
deleted_branches.each do |branch_name| | |
next if ['master', 'staging'].include? branch_name | |
status = my_branch_statuses.find { |l| l.start_with? "#{branch_name} " } | |
next unless status | |
if status.include? ' [ahead ' | |
keep << status | |
else | |
branches_to_delete << status | |
end | |
end | |
puts 'no branches to delete' and return if branches_to_delete.empty? | |
git_delete = "git branch -D #{branches_to_delete.map {|s| s.split(' ')[0] }.join(' ')}" | |
puts 'branches ready to delete: ' | |
puts branches_to_delete | |
puts git_delete | |
puts ' ' | |
puts 'ready? [y]/n' | |
ready = gets.chomp.to_s.downcase | |
puts ' ' | |
if ready == '' || ready == 'y' || ready == 'yes' | |
puts `#{git_delete}` | |
puts 'doing the actual prune...' | |
`git fetch -p 2>&1` | |
puts ' ' | |
if keep.any? | |
puts 'we kept:' | |
puts keep | |
end | |
else | |
puts 'cool, whatever...' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment