Skip to content

Instantly share code, notes, and snippets.

@nathanl
Created January 20, 2012 16:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nathanl/1648061 to your computer and use it in GitHub Desktop.
Save nathanl/1648061 to your computer and use it in GitHub Desktop.
Clean up tags in a git repository
# 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