切换到另一个 Git 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4330610/
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 09:35:40  来源:igfitidea点击:

Switch to another Git tag

gitgithubgit-tag

提问by jspooner

How do I check out version version/tag 1.1.4 of the rspec bundle?

我如何查看rspec 包的版本/标签 1.1.4 ?

cd ~/Library/Application\ Support/TextMate/Bundles/
git clone git://github.com/rspec/rspec-tmbundle.git RSpec.tmbundle
osascript -e 'tell app "TextMate" to reload bundles'

回答by Fake Code Monkey Rashid

Clone the repository as normal:

照常克隆存储库:

git clone git://github.com/rspec/rspec-tmbundle.git RSpec.tmbundle

Then checkout the tag you want like so:

然后像这样签出你想要的标签:

git checkout tags/1.1.4

This will checkout out the tag in a 'detached HEAD' state. In this state, "you can look around, make experimental changes and commit them, and [discard those commits] without impacting any branches by performing another checkout".

这将签出处于“分离的 HEAD”状态的标签。在这种状态下,“您可以环顾四周,进行实验性更改并提交它们,然后[放弃那些提交],而不会通过执行另一次检出而影响任何分支”。

To retain any changes made, move them to a new branch:

要保留所做的任何更改,请将它们移动到新分支:

git checkout -b 1.1.4-jspooner

You can get back to the master branch by using:

您可以使用以下命令返回主分支:

git checkout master

Note, as was mentioned in the first revision of this answer, there is another way to checkout a tag:

请注意,正如本答案的第一版中提到的,还有另一种签出标签的方法:

git checkout 1.1.4

But as was mentioned in a comment, if you have a branch by that same name, this will result in git warning you that the refname is ambiguous and checking out the branch by default:

但是正如评论中提到的,如果您有一个同名的分支,这将导致 git 警告您 refname 不明确并默认检查分支:

warning: refname 'test' is ambiguous.
Switched to branch '1.1.4'

The shorthand can be safely used if the repository does not share names between branches and tags.

如果存储库不在分支和标签之间共享名称,则可以安全地使用速记。

回答by chharvey

As of Git v2.23.0(August 2019), git switchis preferred over git checkoutwhen you're simply switching branches/tags. I'm guessing they did this since git checkouthad two functions: for switching branches and for restoring files. So in v2.23.0, they added two new commands, git switch, and git restore, to separate those concerns. I would predict at some point in the future, git checkoutwill be deprecated.

Git v2.23.0(2019 年 8 月)开始,git switchgit checkout简单地切换分支/标签更受欢迎。我猜他们这样做是因为git checkout有两个功能:切换分支和恢复文件。因此,在 v2.23.0 中,他们添加了两个新命令git switch、 和git restore来分离这些问题。我会预测在未来的某个时候,git checkout会被弃用。

To switch to a normal branch, use git switch <branch-name>. 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.

要切换到普通分支,请使用git switch <branch-name>. 要切换到类似提交的对象,包括单个提交和标签,请使用git switch --detach <commitish>,其中<commitish>是标签名称或提交编号。

The --detachoption forces you to recognize that you're in a mode of “inspection and discardable experiments”. To create a new branch from the commitish you're switching to, use git switch -c <new-branch> <start-point>.

--detach选项迫使您认识到您正处于“检查和可丢弃的实验”模式中。要从您要切换到的提交创建一个新分支,请使用git switch -c <new-branch> <start-point>.