Git 克隆特定標籤
Stewart Nguyen
2022年4月22日
在本文中,我們將學習如何從遠端倉庫克隆特定標籤。
為此,我們將使用命令 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 git@github.com:stwarts/git-demo.git && 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