Git 淺克隆后拉取其他分支


對於已淺克隆的項目

$ git clone --depth=1 <git-repo-url> repo
$ cd repo

現在淺克隆了一個Git倉庫repo。但倉庫里查詢遠程分支只有一個默認分支(這里是 master ),沒有其他分支(如 weekly ):

$ git branch -r
  origin/HEAD -> origin/master
  origin/master

查看git config:

$ git config --get remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master

此時是無法拉取其他分支的。解決步驟如下:

$ git remote set-branches origin *

$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

$ git fetch --depth=1 origin weekly
remote: Enumerating objects: 31, done.
...
 * branch            weekly     -> FETCH_HEAD
 * [new branch]      weekly     -> origin/weekly

$ git branch -r
  origin/HEAD -> origin/master
  origin/master
  origin/weekly

$ git checkout -b weekly origin/weekly
Switched to a new branch 'weekly'
branch 'weekly' set up to track 'origin/weekly'.

$ git branch -vv
  master 908e654 [origin/master] xxxx
* weekly 2b54b23 [origin/weekly] xxxx

現在已經成功拉取 weekly 分支了。

對於尚未克隆的項目

當你使用 --depth 標志克隆項目時,Git 會默認使用 --single-branch 標志。但你可以使用 --no-single-branch 標志告訴 Git 從每個分支拉取指定深度的歷史記錄。

$ git clone --depth=1 --no-single-branch <git-repo-url> repo

驗證一下克隆結果:

$ cd repo

$ git branch -r
  origin/HEAD -> origin/master
  origin/dev
  origin/master
  origin/weekly

$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

$ git checkout -b weekly origin/weekly
Switched to a new branch 'weekly'
branch 'weekly' set up to track 'origin/weekly'.

優雅~

關於淺克隆的考量

Git 的淺克隆能幫我們節省時間和硬盤空間,不過這也得考慮到本地的網速和硬盤容量。如果這兩者並不是問題,那淺克隆也並非必須的。

另一個需考慮的因素是,即使我們可以從淺克隆的本地倉庫推送代碼至遠程倉庫,但兩者的內容並不完全一致,由於本地和遠程服務器間的計算,每次提交可能需要花費更多時間。
如果你經常從本地提交代碼,那么使用完整克隆是有意義的,同時也便於查看歷史記錄。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM