如何克隆一个特定的Git标签

git-clone(1)手册页

--branch也可以接受标记,并在结果存储库中提交时分离HEAD。

我试着

git clone --branch <tag_name> <repo_url>

但这并不奏效。它返回:

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

如何使用该参数?

451352 次浏览
git clone --depth 1 --branch <tag_name> <repo_url>

--depth 1是可选的,但如果你只需要那个修订的状态,你可能想跳过下载那个修订之前的所有历史。

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

使用--single-branch选项只有克隆历史导致尖端的标签。这样可以避免大量不必要的代码被克隆。

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

使用命令

git clone --help

查看git是否支持该命令

git clone --branch tag_name

如果没有,请执行以下步骤:

git clone repo_url
cd repo
git checkout tag_name

克隆一个特定的标记,可能会返回“分离头”状态

作为一种解决方法,首先尝试克隆repo,然后签出特定的标记。例如:

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 <path>,而不是--work-tree--git-dir

git clone --depth 1 --branch <tag_name> <repo_url>

例子

git克隆——深度1——分支0.37.2 https://github.com/apache/incubator-superset.git < / p >

<tag_name> : 0.37.2


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