Git
是否可以只複製 git 項目的一部分?
我找到了 slackbuilds 的集合,我需要的一些在 GitHub 上。 https://github.com/PhantomX/slackbuilds/ 我不想得到所有的 git。
git clone https://github.com/PhantomX/slackbuilds.git
但是對於這個,只需要一個 slackbuild 。
這該怎麼做?是否可以?
您最終將下載整個歷史記錄,因此我看不出它有多大好處,但您可以使用“稀疏”結帳來結帳特定部分。引用此 Stack Overflow 文章:
進行稀疏複製的步驟如下:
mkdir <repo> cd <repo> git init git remote add -f origin <url>
我要在這裡打斷一下。由於我引用另一篇文章,我不想編輯引用的部分,但不要使用
-f
withgit remote add
。它將執行一次提取,這將拉入整個歷史記錄。只需添加遙控器而無需獲取:git remote add origin <url>
然後像後面描述的那樣做一個淺層提取。
這將使用您的遙控器創建一個空儲存庫,並獲取所有對象但不檢出它們。然後做:
git config core.sparseCheckout true
現在您需要定義要實際簽出的文件/文件夾。這是通過在 中列出它們來完成的
.git/info/sparse-checkout
,例如:echo "some/dir/" >> .git/info/sparse-checkout echo "another/sub/tree" >> .git/info/sparse-checkout
使用淺複製可能會更好。而不是正常的
git pull
,請嘗試:git pull --depth=1 origin master
我最近有機會再次測試這個,試圖只獲得Ubuntu Mono Powerline fonts。上述步驟最終下載了大約 11 MB,其中 Ubuntu 字型本身約為 900 KB:
% git pull --depth=1 origin master remote: Enumerating objects: 310, done. remote: Counting objects: 100% (310/310), done. remote: Compressing objects: 100% (236/236), done. remote: Total 310 (delta 75), reused 260 (delta 71), pack-reused 0 Receiving objects: 100% (310/310), 10.40 MiB | 3.25 MiB/s, done. Resolving deltas: 100% (75/75), done. From https://github.com/powerline/fonts * branch master -> FETCH_HEAD * [new branch] master -> origin/master % du -hxd1 . 11M ./.git 824K ./UbuntuMono 12M .
一個普通
clone
的大約需要 20 MB。有一些儲蓄,但還不夠。在
--filter
Ciro Santilli 的回答中使用+ checkout 方法確實可以減小大小,但是正如那裡提到的那樣,一個一個地下載每個 blob,這很慢:% git fetch --depth=1 --filter=blob:none remote: Enumerating objects: 52, done. remote: Counting objects: 100% (52/52), done. remote: Compressing objects: 100% (49/49), done. remote: Total 52 (delta 1), reused 35 (delta 1), pack-reused 0 Receiving objects: 100% (52/52), 14.55 KiB | 1.32 MiB/s, done. Resolving deltas: 100% (1/1), done. From https://github.com/powerline/fonts * [new branch] master -> origin/master * [new branch] terminus -> origin/terminus % git checkout origin/master -- UbuntuMono remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 1.98 KiB | 1.98 MiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 581 bytes | 581.00 KiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 121.43 KiB | 609.00 KiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 100.66 KiB | 512.00 KiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 107.62 KiB | 583.00 KiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 112.15 KiB | 791.00 KiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 454 bytes | 454.00 KiB/s, done. remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 1 (delta 0), pack-reused 0 Receiving objects: 100% (1/1), 468 bytes | 468.00 KiB/s, done. % du -hxd1 . 692K ./.git 824K ./UbuntuMono 1.5M .
TL;DR:使用全部
--filter
、稀疏檢出和淺層複製來減少總下載量,或者如果您不關心總下載量並且只想要一個目錄,但可能會獲得它,則僅使用稀疏檢出 + 淺層複製。