128

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?

zachd1_618
  • 4,210
  • 6
  • 34
  • 47
  • Do you want to "rewind the branch" (i.e., *remove* commits), or add a *new* commit to the current branch, that sets up the branch so that all files are "the way they were at the commit given by that tag"? – torek Aug 20 '13 at 21:45
  • Either should get the job done, though I think I would rather add a new commit to the current branch in the manner you describe – zachd1_618 Aug 21 '13 at 00:50
  • In that case, use the `git checkout .` method from [this answer](http://stackoverflow.com/a/4114122/1256452). – torek Aug 21 '13 at 01:10

5 Answers5

177

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jurglic
  • 3,599
  • 1
  • 17
  • 13
  • 40
    I think "git revert " reverts the changes in the tagged commit, instead of restoring your working copy to that version. – Matt Dec 22 '14 at 20:32
  • To also get rid of untracked / unversioned files, follow https://stackoverflow.com/a/912737/923560 – Abdull Feb 15 '18 at 14:48
  • 8
    Like @Matt said, the accepted answer is simply incorrect, as it only reverts the changes in the tagged commit. Your best bet is to use commit hashes, and follow this answer: https://stackoverflow.com/a/4114122/6348485 – Swagga Ting May 26 '20 at 16:56
  • 2
    Hi, could you explain "generate revert commit on top to get you to the state of the tag:"? I have no idea what this means unfortunately. – John Little Sep 20 '20 at 19:23
  • `git revert` just doesn't work. Looks like there's no way without forcing the push. Not the best for a control version software – Christian Vincenzo Traina Feb 07 '23 at 10:24
19

Use git reset:

git reset --hard "Version 1.0 Revision 1.5"

(assuming that the specified string is the tag).

devnull
  • 118,548
  • 33
  • 236
  • 227
6

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
    
ZZZ
  • 704
  • 9
  • 18
4

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>

joshi123
  • 835
  • 2
  • 13
  • 33
0

I've been looking for a solution to this problem for a long time. After a lot of research I can state that there is not a solution to this problem:

  • git reset --hard <tagname> will change the history. It's not a revert
  • git revert <tagname> will only apply the change of that specific commit
  • git revert <hash1>..<hash2> won't work if there are merge. And even if you can specify the parent, this operation may be tedious if there are several merges. We have also to consider that every single revert must be wisely chosen and tested, if we don't want to break our branch. git rebase and git cherry-pick have the same problem.

So this is the only thing that worked for me:

git checkout <tag name>
mkdir ../tmp
cp -r . ../tmp
git checkout master
cp -rf ../tmp/* .
rm -rf ../tmp
git commit -m "Revert"
git push