Git 克隆特定标签


为此,我们将使用命令 git clone -b <tag> --single-branch <remote_repository>

  • -b 选项接受你要克隆的标签或分支。
  • --single-branch 选项表示只有选项 -b 提供的标签将被克隆到本地。所有其他远程分支/标签将被忽略。

克隆后 Git 不会为我们创建新的分支; 实际上,它只是指标签的 SHA。

我们有责任使用 git switch -c <new-branch-name> 从标签的 SHA 建立一个新分支。

$ git clone -b v1.0.0 --single-branch && cd git-demo
Cloning into 'git-demo'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 13 (delta 1), reused 8 (delta 0), pack-reused 0
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (1/1), done.
Note: switching to '9265e3bd97863fde0a13084f04163ceceff9a9d0'.
You are in a `detached HEAD` state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you have created, you may
do so (now or later) by using -c with the switch command. Example:
  git switch -c <new-branch-name>
Or undo this operation with:
  git switch -
$ git switch -c branch-off-from-tag-v1.0.0
$ git branch
* branch-off-from-tag-v1.0.0