如何删除已推送的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>'

其他回答

要从远程存储库中删除标记,请执行以下操作:

git push --delete origin TAGNAME

您可能还想在本地删除标记:

git tag -d TAGNAME

似乎xargs已经做了很多工作。回顾这篇文章,我猜你所经历的xargs的缓慢是因为最初的答案使用了xargs-n 1,而实际上并不需要。

除了xargs自动处理最大命令行长度之外,这与方法1等效:

git tag | sorting_processing_etc | xargs git push --delete origin

xargs也可以并行运行进程。使用xargs的方法2:

git tag | sorting_processing_etc | xargs -P 5 -n 100 git push --delete origin

上面最多使用5个进程来处理每个进程中最多100个参数。你可以尝试这些论点,以找到最适合你的需求的东西。

正如@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

用于数千个远程标签的速度快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 " ")

我只是想分享我创建的别名,它做了同样的事情:

将以下内容添加到~/.gitconfig中

[alias]
    delete-tag = "!f() { \
            echo 'deleting tag' $1 'from remote/origin ausing command: git push --delete origin tagName;'; \
            git push --delete origin $1; \
            echo 'deleting tag' $1 'from local using command: git tag -d tagName;'; \
            git tag -d $1; \
        }; f"

用法如下:

-->git delete-tag v1.0-DeleteMe
deleting tag v1.0-DeleteMe from remote/origin ausing command: git push --delete origin tagName;
To https://github.com/jsticha/pafs
 - [deleted]             v1.0-DeleteMe
deleting tag v1.0-DeleteMe from local using command: git tag -d tagName;
Deleted tag 'v1.0-DeleteMe' (was 300d3ef22)