之后,您可能还想删除本地标签。
这要快得多,因此我们可以返回使用 xargs 和 git tag -d ,这已经足够了。
git tag | sorting_processing_etc | xargs -L 1 git tag -d
As @CubanX suggested, I've split this answer from my original:
Here is a method which is several times faster than xargs and may scale much more with tweaking. It uses the Github API, a personal access token, and leverages the utility parallel.
parallel has many operating modes, but generally parallelizes any command you give it while allowing you to set limits on the number of processes. You can alter the --jobs 2 parameter to allow faster operation, but I had problems with Github's rate limits, which are currently 5000/hr, but also seems to have an undocumented short-term limit as well.
After this, you'll probably want to delete your local tags too.
This is much faster so we can go back to using xargs and git tag -d, which is sufficient.
git tag | sorting_processing_etc | xargs -L 1 git tag -d
git tag -l | grep -P '^(?!Production-2017-0[89])' | xargs git tag -d
I wanted to remove all tags except for those that match a pattern so that I could delete all but the last couple of months of production tags, here's what I used to great success:
Delete All Remote Tags & Exclude Expression From Delete
Seems like a lot of work for something xargs already does. Looking back through this thread, I'm guessing the slowness with xargs that you experienced is because the original answer used xargs -n 1 when it didn't really need to.
This is equivalent to your method one except that xargs automatically deals with the maximum command line length:
git tag | sorting_processing_etc | xargs git push --delete origin
xargs can run processes in parallel too. Method 2 with xargs:
The above uses a maximum of 5 processes to handle a maximum of 100 arguments in each process. You can experiment with the arguments to find what works best for your needs.
-->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)
Just wanted to share an alias I created which does the same thing:
-->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)
You can push an 'empty' reference to the remote tag name:
git push origin :tagname
Or, more expressively, use the --delete option (or -d if your git version is older than 1.8.0):
git push --delete origin tagname
Note that git has tag namespace and branch namespace so you may use the same name for a branch and for a tag. If you want to make sure that you cannot accidentally remove the branch instead of the tag, you can specify full ref which will never delete a branch:
git push origin :refs/tags/tagname
If you also need to delete the local tag, use:
git tag --delete tagname
Background
Pushing a branch, tag, or other ref to a remote repository involves specifying "which repo, what source, what destination?"
git push remote-repo source-ref:destination-ref
A real world example where you push your master branch to the origin's master branch is:
git push --delete origin $TAGNAME is the correct approach (in addition of a local delete).
But: make sure to use Git 2.31+ (Q1 2021).
"git push $there --delete"(man) should have been diagnosed as an error, but instead turned into a matching push, which has been corrected with Git 2.31 (Q1 2021).
push: do not turn --delete '' into a matching push
Noticed-by: Tilman Vogel
When we added a syntax sugar "git push remote --delete"(man)<ref> to "git push"(man) as a synonym to the canonical git push remote(man) : syntax at f517f1f ("builtin-push: add(man)--delete as syntactic sugar for :foo", 2009-12-30, Git v1.7.0-rc0 -- merge), we weren't careful enough to make sure that <ref> is not empty.
Blindly rewriting "--delete <ref>" to ":<ref>" means that an empty string <ref> results in refspec ":", which is the syntax to ask for "matching" push that does not delete anything.
Worse yet, if there were matching refs that can be fast-forwarded, they would have been published prematurely, even if the user feels that they are not ready yet to be pushed out, which would be a real disaster.
If you use SourceTree - a great Git GUI - then you can easily do this without the command line by doing the following:
Open your repository in SourceTree
Select and expand the "Tags" tab on the left
Right-Click on the tag you want deleted
Select "Delete YOUR_TAG_NAME"
In the verification window, select "Remove Tag From Remotes"
YOUR_TAG_NAME will now be removed from your local repository and all remotes - be it GitHub, BitBucket, or wherever else you listed as a remote for that repository.
Also, if you deleted a tag locally but not on the remote origins, and you want to delete it everywhere, then just create a new tag that has the same name and is attached at the same commit as the origins. Then, repeat the steps above to delete everywhere.
在这两种方法之后,您可能想要也删除您的本地标签。
这要快得多,因此我们可以返回使用 xargs 和 git tag -d ,这已经足够了。
git tag | sorting_processing_etc | xargs -L 1 git tag -d
或者类似于远程删除:
git tag -d $(< git tag | sorting_processing_etc | paste -sd " ")
Up to 100x faster method for thousands of remote tags
After reading through these answers while needing to delete over 11,000 tags, I learned these methods relying or xargs take far too long, unless you have hours to burn.
Struggling, I found two much faster ways. For both, start with git tag or git ls-remote --tags to make a list of tags you want to delete on the remote. In the examples below you can omit or replace sorting_proccessing_etc with any greping, sorting, tailing or heading you want (e.g.grep -P "my_regex" | sort | head -n -200etc) :
This first method is by far the fastest, maybe 20 to 100 times faster than using xargs, and works with a least several thousand tags at a time.
git push origin $(< git tag | sorting_processing_etc \
| sed -e 's/^/:/' | paste -sd " ") #note exclude "<" for zsh
How does this work?
The normal, line-separated list of tags is converted to a single line of space-separated tags, each prepended with : so . . .
tag1 becomes
tag2 ======> :tag1 :tag2 :tag3
tag3
Using git push with this format tag pushes nothing into each remote ref, erasing it (the normal format for pushing this way is local_ref_path:remote_ref_path).
After both of these methods, you'll probably want to delete your local tags too.
This is much faster so we can go back to using xargs and git tag -d, which is sufficient.
git tag | sorting_processing_etc | xargs -L 1 git tag -d
OR similar to the remote delete:
git tag -d $(< git tag | sorting_processing_etc | paste -sd " ")
To https://github.com/SCHKN/repo.git
- [deleted] v1.0
要删除远程 Git 标签,请使用 git push 命令并使用 refs 语法指定标签名称。
git push origin :refs/tags/<tag_name>
回到示例,要删除名为 v1.0 的标签,您将运行。
git push origin :refs/tags/v1.0
To https://github.com/SCHKN/repo.git
- [deleted] v1.0
为什么我们应该指定refs/tags而不是仅仅指定标签名称?
有时,您的标签可能与您的分支具有相同的名称。
如果您尝试删除 Git 标签而不指定 refs/tags,您将收到以下错误。
git push origin :v1.0
error: dst refspec v1.0 matches more than one.
error: failed to push some refs to '<repository>'
因此,您需要指定您正在尝试删除 Git 标签而不是 Git 存储库。
Update 2023
First, delete a tag from the local repository.
To delete a local Git tag, use the git tag command with the -d option.
git tag -d <tag_name>
For example, you would run if you wanted to delete a local tag named v1.0 on your commit list.
git tag -d v1.0
If deleting is successful, you can see something like this output.
Deleted tag 'v1.0' (was 808b598)
List your existing tags using the tag command and the -l option to ensure that tags were correctly deleted.
git tag -l
Second, delete the tag from the remote repository.
To delete a remote Git tag, use the “git push” command with the “–delete” option and specify the tag name.
git push --delete origin <tag_name>
Back to the previous example, if you want to delete the remote Git tag named “v1.0”, you would run.
git push --delete origin v1.0
If deleting is successful, you can see something like this output.
To https://github.com/SCHKN/repo.git
- [deleted] v1.0
To delete a remote Git tag, use the git push command and specify the tag name using the refs syntax.
git push origin :refs/tags/<tag_name>
Back to the example, to delete a tag named v1.0, you would run.
git push origin :refs/tags/v1.0
To https://github.com/SCHKN/repo.git
- [deleted] v1.0
Why should we specify the refs/tags instead of just specifying the tag name?
Sometimes, your tag may have the same name as your branch.
If you tried to delete your Git tag without specifying the refs/tags you would get the following error.
git push origin :v1.0
error: dst refspec v1.0 matches more than one.
error: failed to push some refs to '<repository>'
Consequently, you need to specify that you are trying to delete a Git tag and not a Git repository.
发布评论
评论(28)
如果你使用PowerShell,并且你想删除一堆:
当然,你也可以在删除之前过滤它们:
If you're using PowerShell, and you want to delete a bunch of them:
Of course, you can also filter them before deleting:
正如 @CubanX 所建议的,我已经将这个答案与原来的答案分开:
这是一种比 xargs 快几倍的方法,并且可以通过调整来扩展更多。它使用 Github API、个人访问令牌和利用实用程序
parallel
。parallel
有多种操作模式,但通常可以并行化任何模式你给它的命令同时允许你设置进程数量的限制。您可以更改--jobs 2
参数以允许更快的操作,但我在使用 Github 的 速率限制,目前为 5000/小时,但似乎也有未记录的短期限制。之后,您可能还想删除本地标签。
这要快得多,因此我们可以返回使用 xargs 和 git tag -d ,这已经足够了。
As @CubanX suggested, I've split this answer from my original:
Here is a method which is several times faster than
xargs
and may scale much more with tweaking. It uses the Github API, a personal access token, and leverages the utilityparallel
.parallel
has many operating modes, but generally parallelizes any command you give it while allowing you to set limits on the number of processes. You can alter the--jobs 2
parameter to allow faster operation, but I had problems with Github's rate limits, which are currently 5000/hr, but also seems to have an undocumented short-term limit as well.After this, you'll probably want to delete your local tags too.
This is much faster so we can go back to using
xargs
andgit tag -d
, which is sufficient.我想删除除与模式匹配的标签之外的所有标签,以便我可以删除除最后几个月之外的所有生产标签,这是我取得巨大成功的方法:
删除所有远程标签和标签。从删除中排除表达式
删除所有本地标签和表达式从删除中排除表达式
I wanted to remove all tags except for those that match a pattern so that I could delete all but the last couple of months of production tags, here's what I used to great success:
Delete All Remote Tags & Exclude Expression From Delete
Delete All Local Tags & Exclude Expression From Delete
对于乌龟 git 用户来说,在数百个标签的规模下,您可以使用 UI 一次删除多个标签,但 UI 很好地隐藏在上下文菜单下。
从资源管理器窗口右键单击 ->浏览参考资料 ->右键单击 ref/refmotes/name ->选择“删除远程标签”
请参阅 https://tortoisegit.org/docs/tortoisegit/tgit-dug-browse-ref.html
For tortoise git users, at a scale of hundreds tags, you can delete multiple tags at once using UI, but the UI is well hidden under context menu.
From explorer windows right click -> Browse references -> Right click on ref/refmotes/name -> choose 'Delete remote tags'
See https://tortoisegit.org/docs/tortoisegit/tgit-dug-browse-ref.html
如果您创建了以#字符开头的标签,例如
#ST002
,您可能会发现无法使用正常模式删除。即不会删除该标签,而是将其包装在 String Literal 中,如下所示,
这会将其删除。希望它能帮助那些犯了使用#书写标签名称错误的人。
If you have a tag created starting with the # character, e.g.
#ST002
, you might find that u are unable to delete using normal patterns. i.e.Will not delete the tag, but wrapping it in a String Literal like so
That will get it deleted. Hoping it will help someone who made the mistake of using # to write tag names.
看起来 xargs 已经做了很多工作。回顾这个线程,我猜测您遇到的
xargs
缓慢是因为原始答案在实际上不需要时使用了xargs -n 1
。这与您的方法一相同,只是
xargs
自动处理最大命令行长度:xargs
也可以并行运行进程。方法 2 使用xargs
:上面最多使用 5 个进程,每个进程最多处理 100 个参数。您可以尝试这些参数,找到最适合您需求的方法。
Seems like a lot of work for something
xargs
already does. Looking back through this thread, I'm guessing the slowness withxargs
that you experienced is because the original answer usedxargs -n 1
when it didn't really need to.This is equivalent to your method one except that
xargs
automatically deals with the maximum command line length:xargs
can run processes in parallel too. Method 2 withxargs
:The above uses a maximum of 5 processes to handle a maximum of 100 arguments in each process. You can experiment with the arguments to find what works best for your needs.
只是想分享我创建的别名,它可以做同样的事情:
将以下内容添加到您的
~/.gitconfig
用法如下:
Just wanted to share an alias I created which does the same thing:
Add the following to your
~/.gitconfig
The usage looks like:
这是一个本地测试用例,可以在本地测试它,而无需干扰远程:
Here is a local testcase to test it locally without messing with a remote:
这里已经有很多好的答案,但是如果您需要删除所有标签,可以使用使用 PowerShell 的这个衬垫:
foreach($tag in (git tag)) { git tag -d $tag 。修剪(); git push origin :refs/tags/$tag }
这里,我们获取所有标签的列表,删除每个本地标签,然后删除远程标签(在 GitHub 上测试)。
There are a lot of good answers here already, but if you ever need to delete all the tags, this one liner using PowerShell can be used:
foreach($tag in (git tag)) { git tag -d $tag.Trim(); git push origin :refs/tags/$tag }
Here, we get the list of all tags, delete each local tag followed by deleting the remote tag (tested on GitHub).
这对我有用
This worked for me
您可以将“空”引用推送到远程标记名称:
或者,更明确地说,使用
--delete
选项(或-d
如果您的 git 版本低于1.8.0):请注意,git 具有标记命名空间和分支命名空间,因此您可以对分支和标记使用相同的名称。如果你想确保你不会意外删除分支而不是标签,你可以指定完整的引用,这将永远不会删除分支:
如果你还需要删除本地标签,请使用:
后台
推送分支、标签或对远程存储库的其他引用涉及指定“哪个存储库、什么源、什么目的地?”
一个将 master 分支推送到源的 master 分支的真实示例是:
由于默认路径,可以缩短为:
标签的工作方式相同:
也可以缩短为:
通过省略源引用(之前的部分)冒号),您将“无”推送到目的地,删除远程端的引用。
You can push an 'empty' reference to the remote tag name:
Or, more expressively, use the
--delete
option (or-d
if your git version is older than 1.8.0):Note that git has tag namespace and branch namespace so you may use the same name for a branch and for a tag. If you want to make sure that you cannot accidentally remove the branch instead of the tag, you can specify full ref which will never delete a branch:
If you also need to delete the local tag, use:
Background
Pushing a branch, tag, or other ref to a remote repository involves specifying "which repo, what source, what destination?"
A real world example where you push your master branch to the origin's master branch is:
Which because of default paths, can be shortened to:
Tags work the same way:
Which can also be shortened to:
By omitting the source ref (the part before the colon), you push 'nothing' to the destination, deleting the ref on the remote end.
更直接的方法是
IMO 前缀冒号语法在这种情况下有点奇怪
A more straightforward way is
IMO prefixing colon syntax is a little bit odd in this situation
如果您有要删除的远程标记
v0.1.0
,并且您的远程标记为origin
,则只需:如果您还需要在本地删除该标记:
请参阅 Adam Franco 的回答,解释了 Git 不寻常的
:
删除语法。If you have a remote tag
v0.1.0
to delete, and your remote isorigin
, then simply:If you also need to delete the tag locally:
See Adam Franco's answer for an explanation of Git's unusual
:
syntax for deletion.删除所有本地标签并获取远程标签列表:
删除所有远程标签
清理本地标签
Delete all local tags and get the list of remote tags:
Remove all remote tags
Clean up local tags
git push --delete origin $TAGNAME 是正确的方法(除了本地删除之外)。
但是:请确保使用 Git 2.31+(2021 年第一季度)。
“
git push $there --delete
“(man) 应该已被诊断出来作为一个错误,而是变成了匹配的推送,并已使用 Git 2.31(2021 年第一季度)进行了更正。请参阅 提交 20e4164(2021 年 2 月 23 日) .com/gitster" rel="noreferrer">Junio C Hamano (
gitster
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 1400458,2021 年 2 月 25 日)git push --delete origin $TAGNAME
is the correct approach (in addition of a local delete).But: make sure to use Git 2.31+ (Q1 2021).
"
git push $there --delete
"(man) should have been diagnosed as an error, but instead turned into a matching push, which has been corrected with Git 2.31 (Q1 2021).See commit 20e4164 (23 Feb 2021) by Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit 1400458, 25 Feb 2021)从您的终端执行以下操作:
现在转到 Github.com 并刷新,它们就会消失。
From your terminal, do this:
Now go to Github.com and refresh, they disappear.
第一行从本地存储库中删除
your_tag_name
,第二行从远程存储库中删除your_tag_name
。对于那些使用 GitHub 的人来说,还需要一步:丢弃草稿。
The first line deletes
your_tag_name
from local repo and second line deletesyour_tag_name
from remote repo.For those who use GitHub, one more step is needed: discarding draft.
这两个步骤效果很好:
This two steps works fine:
要从远程存储库中删除标签:
您可能还想在本地删除标签:
To remove the tag from the remote repository:
You may also want to delete the tag locally:
这对我个人来说是最快的方法:
首先在本地删除:
然后在远程删除:
This is the quickest way for me, personally:
First delete locally:
Then delete on the remote:
删除单个标签:
删除多个标签:
删除与特定模式匹配的标签:
Delete a single tag:
Delete multiple tags:
Delete tags that match a particular pattern:
删除本地标签“12345”
删除远程标签“12345”(例如 GitHub 版本)
替代方法
Delete local tag '12345'
Delete remote tag '12345' (eg; GitHub version too)
alternative approach
请注意,如果您有一个名为远程标记的远程分支,这些命令是不明确的:
因此您必须使用此命令来删除标记:
并使用此命令来删除分支:
如果没有,您将收到如下错误:
Just notice that, if you have a remote branch named as a remote tag, these commands are ambiguous:
So you must use this command to delete the tag:
and this one to delete the branch:
If not, you would get an error like this:
如果您使用 SourceTree - 一个很棒的 Git GUI - 那么您可以通过执行以下操作轻松地在没有命令行的情况下完成此操作:
YOUR_TAG_NAME 现在将从您的本地存储库和所有遥控器中删除 - 无论是 GitHub、BitBucket 还是您列为遥控器的其他任何地方那个存储库。
此外,如果您在本地删除了标签,但不在远程源上删除了标签,并且想要在所有地方删除它,则只需创建一个具有相同名称并附加在与源相同的提交处的新标签即可。然后,重复上述步骤,将所有地方都删除。
If you use SourceTree - a great Git GUI - then you can easily do this without the command line by doing the following:
YOUR_TAG_NAME will now be removed from your local repository and all remotes - be it GitHub, BitBucket, or wherever else you listed as a remote for that repository.
Also, if you deleted a tag locally but not on the remote origins, and you want to delete it everywhere, then just create a new tag that has the same name and is attached at the same commit as the origins. Then, repeat the steps above to delete everywhere.
如果您在 Git 存储库中创建了一个名为
release01
的标签,则可以通过执行以下操作将其从存储库中删除:要从 Mercurial 存储库中删除一个标签:
请参考 https://confluence.atlassian.com/pages/viewpage.action?pageId=282175551
If you have created a tag called
release01
in a Git repository you would remove it from your repository by doing the following:To remove one from a Mercurial repository:
Please reference https://confluence.atlassian.com/pages/viewpage.action?pageId=282175551
针对数千个远程标签的方法速度提高了 100 倍
在阅读这些答案并需要删除超过 11,000 个标签后,我了解到这些方法依赖或
xargs
需要太长时间,除非您有几个小时可以燃烧。经过一番挣扎,我发现了两种更快的方法。对于这两种情况,请从
git tag
或git ls-remote --tags
开始,创建要在远程删除的标签列表。在下面的示例中,您可以省略sorting_proccessing_etc
或将其替换为任何grep
ing、sort
ing、tail
ing 或 < code>head 你想要的(例如grep -P "my_regex" | sort | head -n -200
etc):第一种方法是迄今为止最快的,可能比使用
xargs
快 20 到 100 倍,并且一次可处理至少数千个标签。时间。这是如何运作的?
正常的行分隔标签列表将转换为单行空格分隔标签,每个标签前面都带有
:
so 。 。 。使用带有此格式标记的 git push 不会将任何内容推送到每个远程引用中,并删除它(以这种方式推送的正常格式是 local_ref_path:remote_ref_path) 。
方法二被分解为一个单独的答案同一页面上的其他地方
在这两种方法之后,您可能想要也删除您的本地标签。
这要快得多,因此我们可以返回使用 xargs 和 git tag -d ,这已经足够了。
或者类似于远程删除:
Up to 100x faster method for thousands of remote tags
After reading through these answers while needing to delete over 11,000 tags, I learned these methods relying or
xargs
take far too long, unless you have hours to burn.Struggling, I found two much faster ways. For both, start with
git tag
orgit ls-remote --tags
to make a list of tags you want to delete on the remote. In the examples below you can omit or replacesorting_proccessing_etc
with anygrep
ing,sort
ing,tail
ing orhead
ing you want (e.g.grep -P "my_regex" | sort | head -n -200
etc) :This first method is by far the fastest, maybe 20 to 100 times faster than using
xargs
, and works with a least several thousand tags at a time.How does this work?
The normal, line-separated list of tags is converted to a single line of space-separated tags, each prepended with
:
so . . .Using
git push
with this format tag pushes nothing into each remote ref, erasing it (the normal format for pushing this way islocal_ref_path:remote_ref_path
).Method two is broken out as a separate answer elsewhere on this same page
After both of these methods, you'll probably want to delete your local tags too.
This is much faster so we can go back to using
xargs
andgit tag -d
, which is sufficient.OR similar to the remote delete:
用于从本地和原始位置删除给定标签的简单脚本。
检查标签是否确实存在。
如何使用:
)
Simple script to remove given tag from both local and origin locations.
With a check if tag really exists.
How to use:
)
更新 2023
首先,从本地存储库中删除标签。
要删除本地 Git 标签,请使用带有
-d
选项的git tag
命令。例如,如果您想删除提交列表上名为
v1.0
的本地标记,则可以运行该命令。如果删除成功,您可以看到类似以下的输出。
使用 tag 命令和
-l
选项列出现有标签,以确保正确删除标签。其次,从远程存储库中删除标签。
要删除远程 Git 标签,请使用带有“–delete”选项的“git push”命令并指定标签名称。
回到前面的例子,如果你想删除名为“v1.0”的远程 Git 标签,你可以运行。
如果删除成功,您可以看到类似以下的输出。
要删除远程 Git 标签,请使用 git push 命令并使用 refs 语法指定标签名称。
回到示例,要删除名为
v1.0
的标签,您将运行。为什么我们应该指定
refs/tags
而不是仅仅指定标签名称?有时,您的标签可能与您的分支具有相同的名称。
如果您尝试删除 Git 标签而不指定
refs/tags
,您将收到以下错误。因此,您需要指定您正在尝试删除 Git 标签而不是 Git 存储库。
Update 2023
First, delete a tag from the local repository.
To delete a local Git tag, use the
git tag
command with the-d
option.For example, you would run if you wanted to delete a local tag named
v1.0
on your commit list.If deleting is successful, you can see something like this output.
List your existing tags using the tag command and the
-l
option to ensure that tags were correctly deleted.Second, delete the tag from the remote repository.
To delete a remote Git tag, use the “git push” command with the “–delete” option and specify the tag name.
Back to the previous example, if you want to delete the remote Git tag named “v1.0”, you would run.
If deleting is successful, you can see something like this output.
To delete a remote Git tag, use the
git push
command and specify the tag name using therefs
syntax.Back to the example, to delete a tag named
v1.0
, you would run.Why should we specify the
refs/tags
instead of just specifying the tag name?Sometimes, your tag may have the same name as your branch.
If you tried to delete your Git tag without specifying the
refs/tags
you would get the following error.Consequently, you need to specify that you are trying to delete a Git tag and not a Git repository.