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


当前回答

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

将以下内容添加到~/.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)

其他回答

如果您创建了一个以#字符开头的标记,例如#ST002,您可能会发现无法使用正常模式删除。即

git tag -d #STOO2

不会删除标记,而是将其包装为字符串文字

git tag -d "#ST002" or git tag -d '#ST002'

这将使其被删除。希望它能帮助那些错误使用#来写标签名的人。

如果您有一个远程标签v0.1.0要删除,并且您的远程标签是源,那么只需:

git push origin :refs/tags/v0.1.0

如果您还需要在本地删除标记:

git tag -d v0.1.0

有关Git异常的解释,请参见Adam Franco的回答:删除语法。

这里已经有很多很好的答案,但如果您需要删除所有标记,可以使用以下使用PowerShell的命令行:

foreach($tag in(git tag)){git tag-d$tag.Trim();git push origin:refs/tags/$tag}

在这里,我们得到所有标签的列表,删除每个本地标签,然后删除远程标签(在GitHub上测试)。

这里有一个本地测试用例,可以在本地测试而不干扰远程设备:

~/p $ mkdir gittest    
~/p/git $ cd gittest/
~/p/gittest $ git init
Initialized empty Git repository in /Users/local_user/p/gittest/.git/
 ~/p/gittest $ touch testfile.txt
 ~/p/gittest $ git add testfile.txt
 ~/p/gittest $ git commit -m "initial commit"
[master (root-commit) 912ce0e] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testfile.txt
 ~/p/gittest $ git tag
 ~/p/gittest $ git tag -a testtag
 ~/p/gittest $ git tag
testtag
 ~/p/gittest $ git show-ref
912ce0e40635c90241fdab756dce7ea34938de57 refs/heads/master
b0a6c15cabb990e6d6c46f762891b63608d962f3 refs/tags/testtag
 ~/p/gittest $ cd ..
 ~/p $ mkdir gitbare
 ~/p $ cd gitbare
 ~/p/gitbare $ git init --bare
Initialized empty Git repository in /Users/local_user/p/gitbare/
 ~/p/gitbare $ cd ..
 ~/p $ cd gittest/
 ~/p/gittest $ git remote add origin /Users/local_user/p/gitbare
 ~/p/gittest $ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 215 bytes | 215.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/local_user/p/gitbare
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
 ~/p/gittest $ git push origin testtag
Counting objects: 1, done.
Writing objects: 100% (1/1), 163 bytes | 163.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To /Users/local_user/p/gitbare
 * [new tag]         testtag -> testtag
 ~/p/gittest $ git show-ref
912ce0e40635c90241fdab756dce7ea34938de57 refs/heads/master
912ce0e40635c90241fdab756dce7ea34938de57 refs/remotes/origin/master
b0a6c15cabb990e6d6c46f762891b63608d962f3 refs/tags/testtag
 ~/p/gittest $ git push -d origin testtag
To /Users/local_user/p/gitbare
 - [deleted]         testtag
 ~/p/gittest    git tag -d testtag
Deleted tag 'testtag' (was b0a6c15)
 ~/p/gittest $ git show-ref
912ce0e40635c90241fdab756dce7ea34938de57 refs/heads/master
912ce0e40635c90241fdab756dce7ea34938de57 refs/remotes/origin/master
 ~/p/gittest

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

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