【问题标题】:How to clone a specific Git tag如何克隆特定的 Git 标签
【发布时间】:2013-12-15 08:28:30
【问题描述】:

来自git-clone(1) Manual Page

--branch 还可以在结果存储库中的该提交处获取标签并分离 HEAD。

我试过了

git clone --branch <tag_name> <repo_url>

但它不起作用。它返回:

warning: Remote branch 2.13.0 not found in upstream origin, using HEAD instead

这个参数怎么用?

【问题讨论】:

  • 你是对的,但差别不大。当我问这个问题时,在我的情况下,我需要在一行中执行此操作并且必须使用clone,我被困在“为什么 --branch 不起作用”。该网址的最佳答案使用clone->checkout,这无法解决我的问题。 :)

标签: git git-clone git-tag


【解决方案1】:

如果您的意图是在本地处理代码,您需要以保留最新标记和最新未标记代码更改的方式提取代码。如果您克隆深度为 1 且 HEAD 未标记,您将获得一个没有标记的存储库克隆。

所以要获取带有最新标签的最新代码,我建议这样做:

git clone --depth 50 <repo_url>

基本上你在这里说的是......“不要克隆所有历史......只要给我最近的 50 次提交。”如果你正在处理代码,提交是通常是非常小的文本(所以 50 不是那么大)。数字 50 是任意的……你想要的是一个足够深的深度,可以为你提供你正在寻找的标签。

上面的命令隐含地在存储库的主分支上工作。如果您指定--branch &lt;tag&gt;(使用特定标签名称),您可能会遇到另一个问题:在指定标签之后 分支中没有最新的代码更改。您可以改用--branch &lt;branch&gt; 形式来避免这种情况。一个小而重要的区别是,在指定分支名称时,您将获得所有最近的活动(而不仅仅是运行到特定标签的提交)。

另一个需要考虑的场景:如果您想跟踪远程存储库中的两个分支怎么办?一个带有“v1”标签,最新代码在“v2”中?

在这种情况下,我建议这样做:

git clone --depth 50 --no-single-branch <repo_url>

这句话的意思是:“从每个分支的尖端抓取最后 50 次提交。”这是很多代码吗?可能不是。你可以在 Github 上看到远程 repo 有多少个分支。如果您想在本地分支之间来回切换,以这种方式克隆将为您提供所需的代码。为了使分支在您的环境中可见,只需执行以下操作:

git checkout --track origin/<branch>

这将为您设置一个跟踪远程分支的本地分支(这可能是您想要的)。这很好用,因为我们在本地 repo 中有每个分支的提示。

【讨论】:

    【解决方案2】:
    git clone --depth 1 --branch <tag_name> <repo_url>
    

    示例

    git clone --depth 1 --branch 0.37.2 https://github.com/apache/incubator-superset.git

    <tag_name> : 0.37.2
    
    <repo_url> : https://github.com/apache/incubator-superset.git
    

    【讨论】:

    • 该命令是一个字符一个字符的选择的答案完全相同。
    【解决方案3】:
    git clone --depth 1 --branch <tag_name> <repo_url>
    

    --depth 1 是可选的,但如果您只需要该版本的状态,您可能希望跳过下载该版本之前的所有历史记录。

    【讨论】:

    • 请注意,如果 ref 不明确,并且您有一个分支和一个名称相同的标签,则优先选择该分支。
    • 没有可选的 --depth 1 这与 OP 完全相同还是我错过了什么?
    • @463035818 好像一样,可能是 OP 在遥控器上确实没有任何 2.13.0 标签。
    • 但是标签不是分支。如何获得特定的标签
    • @Melab,来自 git-clone 的手册页,“--branch 还可以在结果存储库中的该提交处获取标签并分离 HEAD”
    【解决方案4】:

    克隆特定标签,可能会返回'detached HEAD'状态

    作为一种解决方法,请先尝试克隆存储库,然后签出特定标签。例如:

    repo_url=https://github.com/owner/project.git
    repo_dir=$(basename $repo_url .git)
    repo_tag=0.5
    
    git clone --single-branch $repo_url # using --depth 1 can show no tags
    git --work-tree=$repo_dir --git-dir=$repo_dir/.git checkout tags/$repo_tag
    

    注意:由于 Git 1.8.5,您可以使用-C &lt;path&gt;,而不是--work-tree--git-dir

    【讨论】:

      【解决方案5】:

      使用--single-branch 选项仅克隆导致标记提示的历史记录。这样可以避免大量不必要的代码被克隆。

      git clone <repo_url> --branch <tag_name> --single-branch
      

      【讨论】:

      • --single-branch 是否等同于 --depth 1
      • 不,它不等价。 --single-branch 克隆整个分支的历史记录。使用 --depth 1 根本不会克隆任何历史记录。
      • 在使用--depth 时也隐含了--single-branch。来自手册When creating a shallow clone with the --depth option, this is the default
      【解决方案6】:

      使用命令

      git clone --help
      

      查看你的git是否支持该命令

      git clone --branch tag_name
      

      如果没有,请执行以下操作:

      git clone repo_url 
      cd repo
      git checkout tag_name
      

      【讨论】:

        【解决方案7】:
        git clone -b 13.1rc1-Gotham  --depth 1  https://github.com/xbmc/xbmc.git
        Cloning into 'xbmc'...
        remote: Counting objects: 17977, done.
        remote: Compressing objects: 100% (13473/13473), done.
        Receiving objects:  36% (6554/17977), 19.21 MiB | 469 KiB/s    
        

        会比:

        git clone https://github.com/xbmc/xbmc.git
        Cloning into 'xbmc'...
        remote: Reusing existing pack: 281705, done.
        remote: Counting objects: 533, done.
        remote: Compressing objects: 100% (177/177), done.
        Receiving objects:  14% (40643/282238), 55.46 MiB | 578 KiB/s
        

        或者

        git clone -b 13.1rc1-Gotham  https://github.com/xbmc/xbmc.git
        Cloning into 'xbmc'...
        remote: Reusing existing pack: 281705, done.
        remote: Counting objects: 533, done.
        remote: Compressing objects: 100% (177/177), done.
        Receiving objects:  12% (34441/282238), 20.25 MiB | 461 KiB/s
        

        【讨论】:

        • --depth 1 是一个 gem,所以很多人下载整个 git 历史只是为了使用HEAD
        • --depth 1 应设为默认值;如果有人尝试检查以前的提交,则应提示他们下载其余提交。
        猜你喜欢
        • 1970-01-01
        • 2021-12-10
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2015-07-10
        • 2010-12-27
        相关资源
        最近更新 更多