How do you revert to a specific tag in Git?

GitTagsTaggingRevert

Git Problem Overview


I know how to revert to older commits in a Git branch, but how do I revert back to a branch's state dictated by a tag? I envision something like this:

git revert -bytag "Version 1.0 Revision 1.5"

Is this possible?

Git Solutions


Solution 1 - Git

Git tags are just pointers to the commit. So you use them the same way as you do HEAD, branch names or commit sha hashes. You can use tags with any git command that accepts commit/revision arguments. You can try it with git rev-parse tagname to display the commit it points to.

In your case you have at least these two alternatives:

  1. Reset the current branch to specific tag:

     git reset --hard tagname
    
  2. Generate revert commit on top to get you to the state of the tag:

     git revert tag
    

This might introduce some conflicts if you have merge commits though.

Solution 2 - Git

Use git reset:

git reset --hard "Version 1.0 Revision 1.5"

(assuming that the specified string is the tag).

Solution 3 - Git

You can use git checkout.

I tried the accepted solution but got an error, warning: refname '<tagname>' is ambiguous'

But as the answer states, tags do behave like a pointer to a commit, so as you would with a commit hash, you can just checkout the tag. The only difference is you preface it with tags/:

git checkout tags/<tagname>

Solution 4 - Git

If you are:

  • sure about which commit you want to get back to
  • get rid of all the commits after that
  • apply changes to your remote
  1. reset to a tag named reset-to-here

    git reset --hard reset-to-here
    
  2. push your change to the remote forcing by +

    git push origin +master
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzachd1_618View Question on Stackoverflow
Solution 1 - GitjurglicView Answer on Stackoverflow
Solution 2 - GitdevnullView Answer on Stackoverflow
Solution 3 - Gitjoshi123View Answer on Stackoverflow
Solution 4 - GitZZZView Answer on Stackoverflow