1

I deleted a tag on my repo with the command

git tag -d v1.1

I misunderstood what this would do and thought it would just remove the tag annotation. Instead it rolls it back to where that tag was added which was quite a few commits a go. I didn't realise this and I then re-tagged it thinking I was at the most current commit

git tag -a v1.1 -m "Merged development branch back into master branch."

Now my repo is rolled back to when that original tag was added and I am not sure how to undo this. I have not pushed anything so my remote repo is unaffected. What is the most appropriate way to undo this change?

user1977981
  • 189
  • 1
  • 12

3 Answers3

2

Use the git reflog.
A full detailed answer is found here but ill summary it for you,

How to move HEAD back to a previous location? (Detached head)


git reflog

git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

Every time the HEAD is modified there will be a new entry in the reflog

In your case you need to find out the last commit before removing the tag and check it out, then read the attached answer on how to continue from there.

git reflog
git checkout HEAD@{...}

This will get you back to your desired commit

enter image description here


Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thanks @CodeWizard, one question why are checkout commands in this list? I am not clear what the associated SHAs relate to. – user1977981 Apr 26 '16 at 10:28
  • Every time you change the HEAD (read the full attached answer) and feel free to vote if you liked it. a reflog entry is added. When you do a checkout the HEAD (SHA-1) is updated to the the new value so there will be a record in the reflog. Only HEAD changes are recorded here and checkout is one who make change – CodeWizard Apr 26 '16 at 10:31
  • I have marked it up @CodeWizard but I still don't completely understand I have a few recent commits where a pull request was issued and committed I am unable to see any of these commits in reflog. In the detached head state I presume I am moving only within the same branch. Is that correct? – user1977981 Apr 26 '16 at 13:14
  • ref log will not display commits them self. only references to commits which HEAD was pointing to – CodeWizard Apr 26 '16 at 13:19
  • I can see the shortened SHA reference to all commits in ref log except the commits relating to the branch merging. Not really sure I understand why these are not included. – user1977981 Apr 26 '16 at 13:31
  • 1
    Execute a `git fsck`and you will see all the commits which are not reachable from any branch anymore. Those commit are "lost" = dangling data. – CodeWizard Apr 26 '16 at 13:36
2

If you just deleted the tag (and the terminal scrollback is still available), you should see a message similar to the following:

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

Run the following command to restore the tag:

git update-ref refs/tags/v1.1 c4f3b4b3
friederbluemle
  • 33,549
  • 14
  • 108
  • 109
1

If you have your deleted tag at the remote repo and you are here just looking for the answer to this question:

How do I undo a local deletion of a tag on github?

Simply do:

git pull

and it will return the deleted tag to your local list of tags.

Alex Gongadze
  • 191
  • 1
  • 5