如何删除已推送的Git标记?
当前回答
要删除远程存储库上的标记,可以使用
git push <remote> :refs/tags/<tagname>
解释上述内容的方法是将其读取为空值,即冒号被推送到远程标记名称之前的值。
其他回答
从本地和源位置删除给定标记的简单脚本。检查标签是否真的存在。
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)
正如@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
请注意,如果您有一个名为远程标记的远程分支,那么这些命令是不明确的:
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>'
这里有一个本地测试用例,可以在本地测试而不干扰远程设备:
~/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
如果您创建了一个以#字符开头的标记,例如#ST002,您可能会发现无法使用正常模式删除。即
git tag -d #STOO2
不会删除标记,而是将其包装为字符串文字
git tag -d "#ST002" or git tag -d '#ST002'
这将使其被删除。希望它能帮助那些错误使用#来写标签名的人。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别