我想从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标记只是磁盘上存储在.git/refs/tags子文件夹中的文件。
你可以只cd .git/refs/tags并删除存储在那里的所有文件,使用你最喜欢的删除文件的方法(rm *,从文件资源管理器UI删除等)。
其他回答
斯特凡的答案是不知道如何从远程删除标签。对于windows powershell,可以先删除远程标记,然后删除本地标记。
git tag | foreach-object -process { git push origin --delete $_ }
git tag | foreach-object -process { git tag -d $_ }
要删除所有本地标签,只需运行以下命令
git tag | xargs git tag -d
如果在执行上述命令删除本地标签后,还需要删除远端标签,可以执行以下命令
git ls-remote --tags --refs origin | cut -f2 | xargs git push origin --delete
注意:用远程处理程序替换原点
如果你有很多upstream (origin)标签需要删除,Powershell v7支持parallel foreach:
git tag | foreach-object -Parallel {
git push origin --delete $_
git tag -d $_
}
在一个命令中推删除所有标签可能更有效。特别是如果你有几百个。
在一个合适的非windows shell中,删除所有远程标记:
git tag | xargs -L 1 | xargs git push origin --delete
然后删除所有本地标签:
git tag | xargs -L 1 | xargs git tag --delete
这应该是可以的,只要您的标记名称中没有'。为此,下面的命令应该是可以的。
git tag | xargs -I{} echo '"{}"' | tr \\n \\0 | xargs --null git push origin --delete
git tag | xargs -I{} echo '"{}"' | tr \\n \\0 | xargs --null git tag --delete
其他获取行列表、用引号将它们括起来、使它们成为一行,然后将该行传递给命令的方法可能存在。考虑到这是终极剥猫皮的环境。
我在任何地方都没有找到不需要每个标记调用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一次。