你如何恢复到 Git 中的特定标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18345115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:48:06  来源:igfitidea点击:

How do you revert to a specific tag in Git?

gittagstaggingrevert

提问by zachd1_618

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 分支中较旧的提交,但是如何恢复到由标签指定的分支状态?我设想这样的事情:

git revert -bytag "Version 1.0 Revision 1.5"

Is this possible?

这可能吗?

回答by jurglic

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 tagnameto display the commit it points to.

Git 标签只是指向提交的指针。因此,您可以像使用 HEAD、分支名称或提交 sha 哈希一样使用它们。您可以将标签与任何接受提交/修订参数的 git 命令一起使用。您可以尝试使用它git rev-parse tagname来显示它指向的提交。

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
    
  1. 将当前分支重置为特定标签:

    git reset --hard tagname
    
  2. 在顶部生成还原提交以使您进入标记的状态:

    git revert tag
    

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

如果您有合并提交,这可能会引入一些冲突。

回答by devnull

Use git reset:

使用git reset

git reset --hard "Version 1.0 Revision 1.5"

(assuming that the specified string is the tag).

(假设指定的字符串是标签)。