Created
March 3, 2015 20:46
-
-
Save amacdougall/18db2283abe2b84b8d54 to your computer and use it in GitHub Desktop.
Kill branches which exist on your origin but not locally
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/ruby | |
# delete branches which exist on the origin repo but not locally | |
WHITELIST = [ | |
"master", | |
"production", | |
"earth", | |
"wind", | |
"water", | |
"fire", | |
"heart", | |
"mind" | |
] | |
branch_status = `git remote show origin` | |
status_lines = branch_status.split(/\n/).map(&:strip) | |
remote_branches = status_lines.drop_while {|line| line !~ /^Remote branches:/} | |
remote_branches.shift # also kill "Remote branches:" | |
remote_branches = remote_branches.take_while {|line| line !~ /^Local branch/} | |
remote_branches = remote_branches.map {|line| line.split[0]} | |
local_branches = `git branch`.split(/\n/).map {|s| s.sub(/^\*?\s*/, "")} | |
target_branches = remote_branches - local_branches - WHITELIST | |
target_branches.pop # also kill "Local" | |
# to be safe, try this dry-run version first: | |
# target_branches.each {|branch| puts "git push origin :#{branch}"} | |
target_branches.each {|branch| `git push origin :#{branch}`} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment