如何删除已推送的Git标记?
当前回答
我想删除所有标签,除了那些与模式匹配的标签,这样我就可以删除除最后几个月的生产标签之外的所有标签,这是我取得巨大成功的原因:
删除所有远程标记并从删除中排除表达式
git tag -l | grep -P '^(?!Production-2017-0[89])' | xargs -n 1 git push --delete origin
删除所有本地标记并从删除中排除表达式
git tag -l | grep -P '^(?!Production-2017-0[89])' | xargs git tag -d
其他回答
这对我有用
git push --force origin refs/tags/<tag_name>:refs/tags/<tag_name>
正如@CubanX所建议的,我将这个答案与我的原始答案分开:
这里有一种方法,它比xargs快几倍,并且可以通过调整进行扩展。它使用Github API(一种个人访问令牌),并并行利用该实用程序。
git tag | sorting_processing_etc | parallel --jobs 2 curl -i -X DELETE \
https://api.github.com/repos/My_Account/my_repo/git/refs/tags/{} -H
\"authorization: token GIT_OAUTH_OR_PERSONAL_KEY_HERE\" \
-H \"cache-control: no-cache\"`
parallel有许多操作模式,但通常会将您给出的任何命令并行化,同时允许您设置进程数量的限制。您可以更改--jobs 2参数以允许更快的操作,但我对Github的速率限制有问题,目前为5000/hr,但似乎也有未记录的短期限制。
在此之后,您可能还想删除本地标记。这要快得多,所以我们可以返回使用xargs和git标记-d,这就足够了。
git tag | sorting_processing_etc | xargs -L 1 git tag -d
从本地和源位置删除给定标记的简单脚本。检查标签是否真的存在。
if [ $(git tag -l "$1") ]; then
git tag --delete $1
git push --delete origin $1
echo done.
else
echo tag named "$1" was not found
fi
如何使用:
创建shell脚本文件(例如gittagpurge.sh)并粘贴内容。chmod脚本文件以使其可执行。使脚本全局可用cd到git项目调用脚本(例如$>git-tag-purge.sh tag_name)
对于乌龟数字用户,在数百个标记的范围内,您可以使用UI一次删除多个标记,但UI隐藏在上下文菜单下。
从资源管理器窗口右键单击->浏览引用->右键单击ref/remotes/name->选择“删除远程标记”
看见https://tortoisegit.org/docs/tortoisegit/tgit-dug-browse-ref.html
请注意,如果您有一个名为远程标记的远程分支,那么这些命令是不明确的:
git push origin :tagname
git push --delete origin tagname
因此,必须使用此命令删除标记:
git push origin :refs/tags/<tag>
这一个删除分支:
git push origin :refs/heads/<branch>
如果没有,则会出现如下错误:
error: dst refspec <tagname> matches more than one.
error: failed to push some refs to '<repo>'