Reputation: 21256
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. 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?
Upvotes: 2
Views: 327
Reputation: 60443
$ git tag -d v1.0.0 Deleted tag 'v1.0.0' (was 1a2b3c4) $
That deleted the local ref 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. 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. You can hang a ref on any object.
Upvotes: 4
Reputation: 2602
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
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
Upvotes: 3