git 如何根据标签签出git中的文件

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

How to checkout a file in git based on a tag

gittags

提问by Newbie

I have a commit hash abcxand I have given it a tag 100. Now I want to checkout a file example.plto that tag 100. Is it possible in git?
I can do git checkout abcs example.pl, but I directly want to checkout a file based on the tag.

我有一个提交哈希abcx,我给了它一个标签100。现在我想将文件签出example.pl到该标签100。在git中有可能吗?
我可以git checkout abcs example.pl,但我想直接根据标签签出一个文件

Can anyone help me with this please?

任何人都可以帮我解决这个问题吗?

回答by cerkiewny

Because of how tags work in Git, they are basically just read only branches which you can treat as any branch in Git. You can address them as tags/<tag>.

由于标签在 Git 中的工作方式,它们基本上只是只读分支,您可以将其视为 Git 中的任何分支。您可以将它们称为tags/<tag>.

To checkout the entire state of the repository to the working directory with a tag, you write:

要使用标签将存储库的整个状态签出到工作目录,请编写:

git checkout tags/<yourtag>

But as with a case of normal branches you can decrease the scope of the checkout to individual files by listing them:

但与普通分支的情况一样,您可以通过列出单个文件来减少对单个文件的检出范围:

git checkout tags/<yourtag> <file1> <file2> ...

The behaviour you are seeing with the log happens because of the fact that Git has two methods of pointing to the repositories. Either your local repository pointer is being changed when you checkout entire repository with:

您在日志中看到的行为是因为 Git 有两种指向存储库的方法。当您使用以下命令检出整个存储库时,您的本地存储库指针正在更改:

git checkout <tag/hash>

or individual statues of the files changes when you check them out to specific state with:

或者当您使用以下命令将文件检出到特定状态时,文件的个别状态会发生变化:

git checkout <tag/hash> filelist

Even if you do checkout all your files to the previous repository state:

即使您将所有文件签出到以前的存储库状态:

git checkout <tag/hash> *

your pointer of local repository won't change. The git log is using the local repository pointer to log the changes on the file so it will be aware of the "future" changes that happened before the state of the file you checked it into. So from the git perspective command:

您的本地存储库指针不会改变。git log 使用本地存储库指针来记录文件的更改,因此它会知道在您签入文件的状态之前发生的“未来”更改。所以从 git 的角度来看命令:

git checkout <tag/hash/branch> file

is not affecting the repository pointer, it is only changing the state of the files to match the specific point in time, if you will run the git status you will see those files as local changes, it doesn't differ in any way from taking the copy paste from the state of the file in the repository.

不影响存储库指针,它只是改变文件的状态以匹配特定的时间点,如果你运行 git status 你会看到这些文件作为本地更改,它与采取没有任何区别从存储库中文件的状态复制粘贴。