我想从Git存储库中删除所有标记。我该怎么做呢?
使用git tag -d tagname在本地删除标签tagname,并使用git push——tags更新git provider上的标签。
我试着:
git tag -d *
但是我看到*表示当前目录中的文件。
$ git tag -d *
error: tag 'file1' not found.
error: tag 'file2' not found.
...
假设我有很多标签,我想全部删除它们。
我想从Git存储库中删除所有标记。我该怎么做呢?
使用git tag -d tagname在本地删除标签tagname,并使用git push——tags更新git provider上的标签。
我试着:
git tag -d *
但是我看到*表示当前目录中的文件。
$ git tag -d *
error: tag 'file1' not found.
error: tag 'file2' not found.
...
假设我有很多标签,我想全部删除它们。
当前回答
并行化和过滤了最新版本
git tag -l "v1.0.*" | xargs -L 1 | xargs git push origin --delete
git fetch origin --prune --prune-tags
第一行,并行地从remote中删除所有匹配的标记。 第二行,从git v2.26.2版本中删除所有已删除的标记,从而更新当前的repo。
为了测试您可以添加的第一行——dry-run,我还鼓励您探索标记列表命令,它有很好的通配符和排除/包含。
其他回答
显示所有包含"v"的标签
git tag -l | grep v | xargs -n 1 sh -c 'echo "Processing tag $0\n" && git show -s $0'
对于使用PowerShell的Windows用户:
git tag | foreach-object -process { git tag -d $_ }
这将通过对返回的每一行执行git tag -d来删除git标签返回的所有标签。
git tag | xargs git tag -d
简单地遵循Unix哲学,即管道处理所有内容。
在Windows上使用相同的命令使用git bash。
我在任何地方都没有找到不需要每个标记调用git push的解决方案,所以我想出了这个变体,在我的情况下,它将运行时间从几个小时减少到几秒钟:
git push --delete origin $( git ls-remote --tags origin | awk '{print $2}' | grep -Ev "\^" | tr '\n' ' ')
解释
git push --delete origin $(...): Deletes a tag (or multiple) on origin $( git ls-remote --tags origin | awk '{print $2}' | grep -Ev "\^" | tr '\n' ' '): Creates a space delimited string of all tags git ls-remote --tags origin: Prints all tags on the remote origin ... | awk '{print $2}' | ...: Only prints the second column of the previous command output ... | grep -Ev "\^" | ...: Filters out unwanted refs/tags/mytag^{} variants (not sure where they come from) ... | tr '\n' ' ': Converts the list into a space delimited string
它利用了这样一个事实,即您可以在空格分隔的字符串中提供多个标记名称,因此它只调用git delete一次。
我必须删除带有前缀的标签
例如,我必须删除标签v0.0.1, v0.0.2, v0.0.3, v0.0.4, v0.0.5
git tag -d $(git tag -l "v0.0.*")
分解并解释上面的语句:
列出所有带有前缀的标签 Git标签-l "v0.0.*" 删除标签 Git标记-d $tag_names
这就是这个表述的原理