在代码开发过程中,可能有些标签创建了但不建议使用,批量移除远程仓库上的标签成为一个麻烦的事情,下面提供了在windows平台上批量删除标签的方式。
Windows上的推荐方法
-
查看所有远程标签
git ls-remote --tags origin -
删除除
'EXCLUDE_TAG1|EXCLUDE_TAG2|...'之外的所有标签git tag -l | Where-Object { $_ -notmatch 'EXCLUDE_TAG1|EXCLUDE_TAG2|...' } | ForEach-Object { git push origin --delete $_ }e,g: 删除除’v1.0.1|v1.0.3’以外的所有标签,忽略不存在的标签删除错误
git tag -l | Where-Object { $_ -notmatch 'v1.0.1|v1.0.3' } | ForEach-Object { git push origin --delete $_ } -
同步本地tag
git fetch --prune --tags
Linux上的推荐方法
- 删除
'EXCLUDE_TAG1|EXCLUDE_TAG2|...'以外的所有标签git tag -l | grep -vE 'EXCLUDE_TAG1|EXCLUDE_TAG2|...' | xargs -n 1 git push --delete origine,g: 删除除’v1.0.1|v1.0.3’以外的所有标签
git tag -l | grep -vE 'v1.0.1|v1.0.3' | xargs -n 1 git push --delete origin
如果目标仓库在Github,需要在删除标签的时候同时删除release
- 先安装
gh cli - 配置
gh能正常操作你的仓库 - 执行如下命令
git tag -l | Where-Object { $_ -notmatch 'EXCLUDE_TAG1|EXCLUDE_TAG2|...' } | ForEach-Object { gh release delete $_ --yes; git push origin --delete $_ }