如何删除已推送的Git标记?


当前回答

请注意,如果您有一个名为远程标记的远程分支,那么这些命令是不明确的:

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>'

其他回答

从本地和源位置删除给定标记的简单脚本。检查标签是否真的存在。

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)

这两个步骤效果很好:

# delete local tag '1.0.0'
git tag -d 1.0.0

# delete remote tag '1.0.0' (eg, GitHub version too)
git push origin :refs/tags/1.0.0

其他答案指出了如何实现这一点,但您应该记住后果,因为这是一个远程存储库。

git标签手册页的On Retagging部分很好地解释了如何礼貌地将更改通知远程回购的其他用户。他们甚至提供了一个方便的公告模板,用于传达其他人应该如何获得您的更改。

删除所有本地标记并获取远程标记列表:

git tag -l | xargs git tag -d
git fetch

删除所有远程标记

git tag -l | xargs -n 1 git push --delete origin

清理本地标记

git tag -l | xargs git tag -d

用于数千个远程标签的速度快100倍

在阅读了这些答案,同时需要删除11000多个标签之后,我了解到这些方法依赖于xargs,或者xargs耗时太长,除非你有几个小时可以燃烧。

在挣扎中,我找到了两种更快的方法。对于这两种情况,从git-tag或git-ls-remote-tag开始,列出要删除的远程标记。在下面的示例中,您可以省略sorting_processing_etc,或将其替换为所需的任何greping、sorting、tailing或head(例如grep-P“my_regex”|sort|head-n-200等):


第一种方法是迄今为止最快的,可能比使用xargs快20到100倍,一次至少可以处理几千个标签。

git push origin $(< git tag | sorting_processing_etc \
| sed -e 's/^/:/' | paste -sd " ") #note exclude "<" for zsh

这是如何工作的?正常的、以行分隔的标记列表被转换为一行以空格分隔的标记,每个标记都以:so。

tag1   becomes
tag2   ======>  :tag1 :tag2 :tag3
tag3

将git push与此格式标记一起使用,不会将任何内容推送到每个远程引用中,并将其删除(以这种方式推送的正常格式是local_ref_path:remote_ref_path)。

方法二在同一页的其他地方作为单独的答案


在这两种方法之后,您可能也希望删除本地标记。这要快得多,所以我们可以返回使用xargs和git标记-d,这就足够了。

git tag | sorting_processing_etc | xargs -L 1 git tag -d

或类似于远程删除:

git tag -d $(< git tag | sorting_processing_etc | paste -sd " ")