Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you revert to a specific tag in Git?

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?

like image 564
zachd1_618 Avatar asked Aug 20 '13 21:08

zachd1_618


People also ask

How do I switch to a specific tag in git?

To switch to a commit-like object, including single commits and tags, use git switch --detach <commitish> , where <commitish> is the tag name or commit number.

Can I go back to a specific commit in git?

Steps to revert a Git commitLocate the ID of the commit to revert with the git log or reflog command. Issue the git revert command and provide the commit ID of interest. Supply a meaningful Git commit message to describe why the revert was needed.

How do I revert master branch to a tag in git?

Another way is to create a diff between the current state of the branch and the tag you want to revert to and then apply that to the branch. This keeps the version history correct and shows the changes going in then coming back out again.


2 Answers

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.

like image 131
jurglic Avatar answered Sep 20 '22 20:09

jurglic


Use git reset:

git reset --hard "Version 1.0 Revision 1.5" 

(assuming that the specified string is the tag).

like image 28
devnull Avatar answered Sep 16 '22 20:09

devnull