Skip to content

Instantly share code, notes, and snippets.

@mu88
Last active July 19, 2022 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mu88/65466df3e7c213f133ce6564c2d1f2b1 to your computer and use it in GitHub Desktop.
Save mu88/65466df3e7c213f133ce6564c2d1f2b1 to your computer and use it in GitHub Desktop.
Delete several tags from Git repo
#!/usr/bin/pwsh
Clear-Host
$repository = "<<Git repository path to work on>>"
$releaseName = "<<Name of release>>"
$tagsToKeep = @("<<Tag to keep>>")
Set-Location -Path $repository
# Get latest tags
git fetch origin
$tags = git tag
foreach ($tag in $tags)
{
# Ignore tags of other releases
if (!($tag.EndsWith($releaseName))) {
Write-Host "'$tag' not relevant because does not match '$releaseName'"
continue
}
# Ignore tags that shall be kept
if ($tagsToKeep -contains $tag) {
Write-Host "'$tag' will be kept and not deleted"
continue
}
# Delete tag from origin
Write-Host "Delete '$tag'"
git push origin --delete $tag
}
git fetch --prune origin "+refs/tags/*:refs/tags/*"
@steffstefferson
Copy link

Line 18: if (!($tag.Contains($releaseName))) { should be if (!($tag.EndsWith($releaseName))) { otherwise when deleting Tags for release 2022-B-Gandalf the tags for 2022-B-Gandalf-Hotfix1 are also deleted.

@mu88
Copy link
Author

mu88 commented Jul 19, 2022

Good catch, thx man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment