简体   繁体   English

在git中,如何撤消本地删除未推送的注释标签?

[英]In git, how can I undo the local deletion of an annotated tag that has not been pushed?

I accidentally deleted an annotated tag locally that I did not intend to delete.我不小心在本地删除了一个我不打算删除的带注释的标签。 I have not yet pushed the tag's deletion to the remote.我还没有将标签的删除推送到遥控器。 I would like to undo this mistake.我想撤消这个错误。

$ git tag -n999
v1.0.0          Initial release

$ git tag -d v1.0.0
Deleted tag 'v1.0.0' (was 1a2b3c4)

I've searched online and here on Stack Overflow in an attempt to find a solution.我已经在网上和 Stack Overflow 上搜索过,试图找到解决方案。 Despite the seeming straightforward nature of this action, I have not had any luck finding a solution.尽管这个动作看起来很简单,但我没有找到解决方案的运气。

How can I undo the local tag deletion, reverting the tag to the state it was before I deleted it?如何撤消本地标签删除,将标签恢复为删除之前的 state?

FYI, I've never done this before prior to answering this question.仅供参考,在回答这个问题之前我从未这样做过。

But after googling your question, I found this article:但在谷歌搜索你的问题后,我发现了这篇文章:

https://dzone.com/articles/git-tip-restore-deleted-tag https://dzone.com/articles/git-tip-restore-deleted-tag

I tested its advice, and it worked for me.我测试了它的建议,它对我有用。


In summary:总之:

Use this to get the list of annotated tag objects that have been removed:使用它来获取已删除的带注释标签对象的列表:

git fsck --unreachable | grep tag

# if don't have grep because you're on windows, you can use this:
git fsck --unreachable | sls tag

Use this to examine the findings from the previous command:使用它来检查上一个命令的结果:

git show KEY

Use this to restore the tag:使用它来恢复标签:

git update-ref refs/tags/NAME KEY
 $ git tag -d v1.0.0 Deleted tag 'v1.0.0' (was 1a2b3c4) $

That deleted the local ref refs/tags/v1.0.0 .这删除了本地参考refs/tags/v1.0.0 To put it back,把它放回去,

git tag v1.0.0 1a2b3c4

That's why the deletion mentioned the object id in the db.这就是为什么删除提到了数据库中的 object id。 Git won't clean objects out of the db until they've been unreachable through any ref for two weeks or more (say git help config and search for gc.*expire), specifically so it doesn't snatch things out from under in-flight operations and ordinary mistakes and second thoughts. Git 不会从数据库中清除对象,直到它们通过任何 ref 无法访问两周或更长时间(比如git help config和搜索 gc.*expire),特别是这样它就不会从下面抢夺东西-飞行操作和普通错误和重新考虑。 You can hang a ref on any object.您可以在任何object 上挂一个参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM