How do you revert to a specific tag in Git?
Asked Answered
D

5

130

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?

Delanty answered 20/8, 2013 at 21:17 Comment(3)
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"?Lipo
Either should get the job done, though I think I would rather add a new commit to the current branch in the manner you describeDelanty
In that case, use the git checkout <id> . method from this answer.Lipo
M
181

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.

Muller answered 21/8, 2013 at 9:49 Comment(5)
I think "git revert <tag>" reverts the changes in the tagged commit, instead of restoring your working copy to that version.Kopans
To also get rid of untracked / unversioned files, follow https://mcmap.net/q/11278/-how-do-i-remove-local-untracked-files-from-the-current-git-working-treeHumour
Like @Kopans 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://mcmap.net/q/11819/-how-do-i-revert-a-git-repository-to-a-previous-commitPrager
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.Wanwand
git revert just doesn't work. Looks like there's no way without forcing the push. Not the best for a control version softwareFloruit
J
19

Use git reset:

git reset --hard "Version 1.0 Revision 1.5"

(assuming that the specified string is the tag).

Jenicejeniece answered 20/8, 2013 at 21:21 Comment(0)
S
7

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
    
Syncopate answered 27/5, 2021 at 18:17 Comment(0)
S
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>

Stricklan answered 4/10, 2020 at 9:46 Comment(4)
I suppose in this way you're getting a detached HEADFloruit
sounds painful! :)Stricklan
It is not, according to Joseph-Ignace GuillotinFloruit
That may be a different problem with a tag and branch of the same name, and the fully qualified tag name is needed. See: #28192922Furness
F
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
Floruit answered 10/5, 2023 at 14:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.