git 从远程存储库中获取单个标签

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

Fetch a single tag from remote repository

gitgit-checkoutgit-taggit-fetch

提问by aleclarson

This command fetches alltags:

此命令获取所有标签:

git fetch origin --tags

This command fetches a specific tag:

此命令获取特定标签:

git fetch origin refs/tags/1.0.0

Butthat doesn't let me do:

但这并不能让我这样做:

git checkout tags/2.3.18

How can I fetch a single tag and then perform a checkout?

如何获取单个标签然后执行结帐?

回答by torek

git fetch origin refs/tags/1.0.0

git fetch origin refs/tags/1.0.0

This fails because it doesn't write a local reference: it obtains the remote's refs/tags/1.0.0, and any tag object(s), commits, etc., required to go with it; it drops those into FETCH_HEAD(as all git fetchcommands always do); and ... that's it. It never creates reference refs/tags/1.0.0in yourrepository, even though it got everything it needed to do so.

这会失败,因为它没有写入本地引用:它获取远程的refs/tags/1.0.0,以及与之一起需要的任何标记对象、提交等;它将它们放入FETCH_HEAD(就像所有git fetch命令一样);和......就是这样。它永远不会refs/tags/1.0.0您的存储库中创建引用,即使它获得了这样做所需的一切。

To make it create such a tag if it does not yet exist:

要让它在尚不存在的情况下创建这样的标签:

git fetch origin refs/tags/1.0.0:refs/tags/1.0.0

The name on the right of the colon is the name your Git will use in your repository. You could turn this tag into a branch named wacky, for instance, by naming it refs/heads/wacky. (There's no reason to do this. I am describing this just for illustration.)

冒号右侧的名称是您的 Git 将在您的存储库中使用的名称。例如,您可以将此标记转换为名为 的分支wacky,例如将其命名为refs/heads/wacky。(没有理由这样做。我描述这个只是为了说明。)

This is a non-forced fetch, so if you already havea refs/tags/1.0.0, your Git will refuse to update your reference. If you wish to overwrite any existing 1.0.0tag, use:

这是一种非强迫取,所以如果你已经一个refs/tags/1.0.0,你的Git会拒绝更新您参考。如果您希望覆盖任何现有1.0.0标签,请使用:

git fetch origin +refs/tags/1.0.0:refs/tags/1.0.0

If you wish to fetch all tags, with or without overwriting:

如果您想获取所有标签,覆盖或不覆盖:

git fetch origin 'refs/tags/*:refs/tags/*'

with or without a leading plus sign. (Note: the quote marks are just to protect the *from your shell. The plus sign may go inside or outside the quotes. In fact, the quotes themselves can go anywhere as long as they surround all asterisks or other shell meta-characters:

带或不带前导加号。(注意:引号只是为了保护*你的 shell。加号可以在引号里面或外面。事实上,引号本身可以放在任何地方,只要它们包围所有的星号或其他 shell 元字符:

refs/tags/'*:refs/tags/*'

or you can use backslashes instead:

或者您可以改用反斜杠:

refs/tags/\*:refs/tags/\*

In all cases we are just protecting the sensitive asterisk from the shell's "asterisks are tasty, let's eat them" functions.)

在所有情况下,我们只是保护敏感的星号免受外壳的“星号很好吃,让我们吃掉它们”功能的影响。)

回答by Olleg

I read all the answers, but there is not yet mentioned one syntactic sugar. If you need to fetch only one tag as a tag (to checkout later) you can write, for instance for refs/tags/2.3.18:

我阅读了所有答案,但还没有提到一种语法糖。如果您只需要获取一个标签作为标签(稍后结帐),您可以编写,例如refs/tags/2.3.18

git fetch origin tag 2.3.18 --no-tags

This is shortcut for already mentioned:

这是已经提到的快捷方式:

git fetch origin refs/tags/2.3.18:refs/tags/2.3.18 --no-tags

Of course, you may not use --no-tagsif you need other tags (according to the default behavior) or if you already explicitly set --no-tagin the clonecommand or in tagOptconfig option(man git-clone).

当然,--no-tags如果您需要其他标签(根据默认行为)或者您已经--no-tagclone命令或tagOpt配置选项( man git-clone) 中明确设置,您可能不会使用。

git fetch origin tag 2.3.18

回答by jthill

When you specify explicit refs to git fetch, you need to either provide a mapping in your refspecor do what you want with the fetched id's recorded in FETCH_HEADyourself.

当您指定显式 refs to 时git fetch,您需要在您的refspec 中提供一个映射,或者使用您FETCH_HEAD自己记录的获取的 id 做您想做的事情。

The advantage to having the no-mapping-provided case do no default processing on the fetched refs is, the no-mapping-provided case does no default processing on the fetched refs. You might appreciate

不提供映射的情况不对获取的引用进行默认处理的优点是,不提供映射的情况不对获取的引用进行默认处理。你可能会欣赏

git fetch origin v2.3.18
git tag v2.3.18 FETCH_HEAD    # ← git tag !$ FE<TAB>

when you really only want just the one tag set up in your local repo and not however many dozens of prior tags in the origin repo point into the fetched history.

当您真的只想要在本地存储库中设置一个标签,而不需要在原始存储库中将许多先前的标记指向获取的历史记录时。