关于git标签:git标签也被推送了吗?

Do git tags get pushed as well?

自从我创建了我的存储库之后,我的标签创建不会被推送到存储库中。当我在本地目录所有标记都存在,但当我登录到远程存储库和做一个git tag,只有前几个出现。

问题可能是什么?.


你可以这样做:

1
git push --tags


在默认的git远程配置中,您必须显式地推送标记(当它们与它们指向的提交一起自动提取时)。你需要使用

1
$ git push <remote> tag <tagname>

推一个标签,或

1
$ git push <remote> --tags

推送所有标签(或git push --tags推送至默认远程,通常为origin)。

这是非常有意的行为,以使推送标记显式化。推标签通常是有意识的选择。

总结Junio C.Hamano所写的内容(链接到@andre miras的评论中)

When fetching, you are interacting with a remote repository somebody has published, which means:

  • the set of tags that exist there are all the publisher wanted people to see, and
  • not only you but other people will also see the same tags.
  • In other words, tags in repositories you fetch from are designed to be public and shared. It will facilitate communication between developers if it is easy for everybody to fetch these same tags.

    这就是为什么git fetch会自动"跟踪"标签,也就是说,它在下载指向的修订时下载标签,换句话说,下载所有相关的已发布标签。

    When pushing, you are pushing from your working repository, which most of the time is not public, and tags in that repository is not designed to be public. You can use your own local tags to mark your progress, so it does not make sense to blindly push all tags in your repository to the repository you are pushing to publish your changes, whose tags are by definition public.

    这就是为什么需要显式地推送标记,将标记标记标记为公共的原因。

    或者,您也可以将您所推的遥控器配置为始终推送所有标签,例如在您的.git/config中放置类似的东西:

    1
    2
    3
    4
    [remote"publish"] # or whatever it is named
        url = ...
        push = +refs/heads/*:refs/heads/*
        push = +refs/tags/*:refs/tags/*

    这意味着强制推送所有头(所有分支)和所有标记(如果不希望强制推送头,请从refspec中删除"+"前缀)。


    请注意,由于git 1.8.3(2013年4月22日),您不再需要执行两个命令来推送分支,然后再推送标签:

    The new"--follow-tags" option tells"git push" to push relevant annotated tags when pushing branches out.

    现在,您可以在推送新提交时尝试:

    1
    git push --follow-tags

    但是,这不会推送所有的本地标签,只会推送提交引用的带注释的标签,这些标签是用git push推送的。

    这一点已由Junio C Hamano(gitster在commit c2aba15中介绍:

    The new option"--follow-tags" tells"git push" to push annotated tags that are missing from the other side and that can be reached by the history that is otherwise pushed out.

    For example, if you are using the"simple","current", or"upstream" push, you would ordinarily push the history leading to the commit at your current HEAD and nothing else.
    With this option, you would also push all annotated tags that can be reached from that commit to the other side.

    配置push.followTags允许默认包含--follow-tags(git 2.4.1+,q2 2015)。nbsp;请参见"同时推送Git提交和标记"


    我通常做的是:

    1
    2
    3
    4
    [remote"publish"] # or whatever it is named
        url = ...
        push = :
        push = +refs/tags/*:refs/tags/*

    这意味着它会推动已经存在的每个分支,加上标签。它不会强制推,也不会推您没有手动推的分支。


    如果要强制获取所有标记,可以通过以下方式在配置中设置:

    1
    git config remote.origin.tagopt --tags

    从文档中:

    Setting this value to --no-tags disables automatic tag following when fetching from remote . Setting it to --tags will fetch every tag
    from remote , even if they are not reachable from remote branch
    heads. Passing these flags directly to git-fetch(1) can override this
    setting. See options --tags and --no-tags of git-fetch(1).