Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Forked from nathanl/git_tag_cleanup.rb
Created June 17, 2014 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaymecd/883d77f61d20c7befb7b to your computer and use it in GitHub Desktop.
Save jaymecd/883d77f61d20c7befb7b to your computer and use it in GitHub Desktop.
# Script to delete all but a specified list of tags
# from a git repo, both locally and remotely
# Run like `mode=test ruby git_tag_cleanup` to test;
# use 'execute' mode to actually delete the tags
mode = ENV['mode'] || 'test'
tags_to_keep = %w[v2.0.0 v2.0.1]
tags_to_delete = `git tag`.split("\n") - tags_to_keep
puts "Keeping: #{tags_to_keep.inspect}"
puts "Deleting: #{tags_to_delete.inspect}"
puts ""
puts mode == 'execute' ? "Executing!" : "NOT executing - test only"
tags_to_delete.each do |tag|
delete_locally = "git tag -d '#{tag}'"
delete_remote = "git push origin :'#{tag}'"
puts delete_locally # to verify output
system(delete_locally) if mode == 'execute'
puts delete_remote
system(delete_remote) if mode == 'execute'
end
puts "\nDone! Tags remaining are:"
puts `git tag`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment