Skip to content

Instantly share code, notes, and snippets.

@igorescobar
Created December 12, 2017 12:03
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 igorescobar/f20bee3240515bb77e7e9e60c9a259b4 to your computer and use it in GitHub Desktop.
Save igorescobar/f20bee3240515bb77e7e9e60c9a259b4 to your computer and use it in GitHub Desktop.
A utility for deleting old git tags
# CUT_OFF_DATE needs to be of YYYY-MM-DD format
CUT_OFF_DATE = "2016-06-30"
def get_old_tags(cut_off_date)
`git log --tags --simplify-by-decoration --pretty="format:%ai %d"`
.split("\n")
.each_with_object([]) do |line, old_tags|
if line.include?("tag: ")
date = line[0..9]
tags = line[28..-2].gsub(",", "").concat(" ").scan(/tag: (.*?) /).flatten
old_tags.concat(tags) if date < cut_off_date
end
end
end
# fetch all tags from the remote
`git fetch`
# delete all tags on the remote that were created before the CUT_OFF_DATE
get_old_tags(CUT_OFF_DATE).each_slice(100) do |batch|
system("git", "push", "--delete", "origin", *batch)
end
# delete all local tags that are no longer present on the remote
`git fetch --prune origin +refs/tags/*:refs/tags/*`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment