是否可以只克隆 git 项目的一部分?

elb*_*rna 52 git

我在 GitHub 上找到了一些 slackbuilds 的集合,我需要一些。 https://github.com/PhantomX/slackbuilds/ 我不想得到所有的 git。

git clone https://github.com/PhantomX/slackbuilds.git
Run Code Online (Sandbox Code Playgroud)

但只得到一个 slackbuild,对于这个

这该怎么做?是否可以?

mur*_*uru 41

您最终将下载整个历史记录,所以我看不出它有多大好处,但是您可以使用“稀疏”结帐来结帐特定部分。引用这篇 Stack Overflow 帖子

进行稀疏克隆的步骤如下:

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>
Run Code Online (Sandbox Code Playgroud)

我要在这里打断一下。由于我引用另一篇文章中,我不想要编辑的报价部分,但千万不能使用-fgit remote add。它将执行一次提取,这将拉入整个历史记录。只需添加遥控器而无需获取:

git remote add origin <url>
Run Code Online (Sandbox Code Playgroud)

然后像后面描述的那样做一个浅取。

这会使用您的遥控器创建一个空的存储库,并获取所有对象但不检查它们。然后做:

git config core.sparseCheckout true
Run Code Online (Sandbox Code Playgroud)

现在您需要定义要实际检出的文件/文件夹。这是通过在 中列出它们来完成的.git/info/sparse-checkout,例如:

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>
Run Code Online (Sandbox Code Playgroud)

[...]

您可能想查看扩展教程,并且您可能应该阅读sparse checkout的官方文档

使用浅克隆可能会更好。而不是一个正常的git pull,尝试:

git pull --depth=1 origin master
Run Code Online (Sandbox Code Playgroud)

我最近有机会再次测试这个,试图只获得Ubuntu Mono Powerline 字体。上述步骤最终下载了大约 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     .
Run Code Online (Sandbox Code Playgroud)

正常clone需要大约 20 MB。有一些节省,但还不够。

使用--filter在西罗桑蒂利的答案+结账方法确实减少了尺寸,但是有提到,下载一个每个斑之一,它是缓慢的:

% 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    .
Run Code Online (Sandbox Code Playgroud)

TL;DR: 使用 all of --filter, sparse checkout 和浅克隆来减少总下载量,或者如果你不关心总下载量并且只想要一个目录,但是它可能会被获取,或者只使用稀疏检出 + 浅层克隆。


Cir*_*郝海东 31

git clone --filter 从 git 2.19 现在在 GitHub 上工作(测试 2021-01-14,git 2.30.0)

此选项是与远程协议的更新一起添加的,它真正阻止了从服务器下载对象。

例如,只克隆d1这个最小测试存储库所需的对象:https : //github.com/cirosantilli/test-git-partial-clone我可以这样做:

git clone \
  --depth 1  \
  --filter=blob:none  \
  --sparse \
  https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git sparse-checkout init --cone
git sparse-checkout set d1
Run Code Online (Sandbox Code Playgroud)

这是https://github.com/cirosantilli/test-git-partial-clone-big-small 上的一个不那么最小和更现实的版本

git clone \
  --depth 1  \
  --filter=blob:none  \
  --sparse \
  https://github.com/cirosantilli/test-git-partial-clone-big-small \
;
cd test-git-partial-clone
git sparse-checkout init --cone
git sparse-checkout set small
Run Code Online (Sandbox Code Playgroud)

该存储库包含:

  • 一个包含 10 个 10MB 文件的大目录
  • 一个包含 1000 个大小为 1 个字节的文件的小目录

所有内容都是伪随机的,因此不可压缩。

我的 36.4 Mbps 互联网上的克隆时间:

  • 满:24s
  • 部分:“瞬间”

sparse-checkout不幸的是,这部分也是需要的。您也可以只下载某些更容易理解的文件:

git clone \
  --depth 1  \
  --filter=blob:none  \
  --no-checkout \
  https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git checkout master -- di
Run Code Online (Sandbox Code Playgroud)

但是由于某种原因,这种方法会非常缓慢地一个一个地下载文件,除非目录中的文件很少,否则无法使用。

分析最小存储库中的对象

clone 命令仅获取:

  • 带有分支尖端的单个提交对象master
  • 存储库的所有 4 个树对象
    • 提交的顶级目录
    • 三个目录d1, d2,master

然后,该git sparse-checkout set命令仅从服务器获取丢失的 blob(文件):

  • d1/a
  • d1/b

更好的是,GitHub 稍后可能会开始支持:

  --filter=blob:none \
  --filter=tree:0 \
Run Code Online (Sandbox Code Playgroud)

其中,--filter=tree:0从Git的2.20将防止不必要的clone获取所有树对象,并允许它推迟到checkout。但是在我 2020-09-18 的测试中失败了:

fatal: invalid filter-spec 'combine:blob:none+tree:0'
Run Code Online (Sandbox Code Playgroud)

大概是因为--filter=combine:复合过滤器(在 Git 2.24 中添加,由 multiple 隐含--filter)尚未实现。

我观察了哪些对象是通过以下方式获取的:

git verify-pack -v .git/objects/pack/*.pack
Run Code Online (Sandbox Code Playgroud)

如在:https : //stackoverflow.com/questions/7348698/git-how-to-list-all-objects-in-the-database/18793029#18793029 中提到的,它没有给我一个超级清楚的指示每个对象是什么确实如此,但它确实说明了每个对象的类型 ( commit, tree, blob),并且由于该最小存储库中的对象很少,因此我可以毫不含糊地推断出每个对象是什么。

git rev-list --objects --all 确实使用树/斑点的路径产生了更清晰的输出,但不幸的是,它在我运行时获取了一些对象,这使得很难确定何时获取了什么,如果有人有更好的命令,请告诉我。

TODO 找到 GitHub 公告,说他们何时开始支持它。https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/从 2020-01-17 已经提到--filter blob:none

git sparse-checkout

我认为这个命令是为了管理一个设置文件,上面写着“我只关心这些子树”,这样以后的命令只会影响这些子树。但是有点难以确定,因为当前的文档有点......稀疏;-)

它本身并不能阻止获取 blob。

如果这种理解是正确的,那么这将是对git clone --filter上述描述的一个很好的补充,因为如果您打算在部分克隆的 repo 中执行 git 操作,它将防止无意中获取更多对象。

当我尝试使用 Git 2.25.1 时:

git clone \
  --depth 1 \
  --filter=blob:none \
  --no-checkout \
  https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git sparse-checkout init
Run Code Online (Sandbox Code Playgroud)

它不起作用,因为init实际上获取了所有对象。

但是,在 Git 2.28 中,它没有按照需要获取对象。但是如果我这样做:

git sparse-checkout set d1
Run Code Online (Sandbox Code Playgroud)

d1未获取和签出,即使这明确表示应该:https : //github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/#sparse-结帐和部分克隆免责声明:

请密切注意部分克隆功能是否普遍可用[1]。

[1]:GitHub 仍在内部评估此功能,同时它已在选定的几个存储库上启用(包括本文中使用的示例)。随着该功能的稳定和成熟,我们会及时向您通报其进展情况。

所以是的,目前很难确定,部分原因是 GitHub 是封闭源代码的乐趣。但让我们密切关注它。

命令分解

服务器应配置为:

git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1
Run Code Online (Sandbox Code Playgroud)

命令分解:

的格式--filter记录在man git-rev-list.

Git 树上的文档:

在本地测试一下

以下脚本可重复地在本地生成https://github.com/cirosantilli/test-git-partial-clone存储库,执行本地克隆,并观察克隆的内容:

#!/usr/bin/env bash
set -eu

list-objects() (
  git rev-list --all --objects
  echo "master commit SHA: $(git log -1 --format="%H")"
  echo "mybranch commit SHA: $(git log -1 --format="%H")"
  git ls-tree master
  git ls-tree mybranch | grep mybranch
  git ls-tree master~ | grep root
)

# Reproducibility.
export GIT_COMMITTER_NAME='a'
export GIT_COMMITTER_EMAIL='a'
export GIT_AUTHOR_NAME='a'
export GIT_AUTHOR_EMAIL='a'
export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000'
export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000'

rm -rf server_repo local_repo
mkdir server_repo
cd server_repo

# Create repo.
git init --quiet
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1

# First commit.
# Directories present in all branches.
mkdir d1 d2
printf 'd1/a' > ./d1/a
printf 'd1/b' > ./d1/b
printf 'd2/a' > ./d2/a
printf 'd2/b' > ./d2/b
# Present only in root.
mkdir 'root'
printf 'root' > ./root/root
git add .
git commit -m 'root' --quiet

# Second commit only on master.
git rm --quiet -r ./root
mkdir 'master'
printf 'master' > ./master/master
git add .
git commit -m 'master commit' --quiet

# Second commit only on mybranch.
git checkout -b mybranch --quiet master~
git rm --quiet -r ./root
mkdir 'mybranch'
printf 'mybranch' > ./mybranch/mybranch
git add .
git commit -m 'mybranch commit' --quiet

echo "# List and identify all objects"
list-objects
echo

# Restore master.
git checkout --quiet master
cd ..

# Clone. Don't checkout for now, only .git/ dir.
git clone --depth 1 --quiet --no-checkout --filter=blob:none "file://$(pwd)/server_repo" local_repo
cd local_repo

# List missing objects from master.
echo "# Missing objects after --no-checkout"
git rev-list --all --quiet --objects --missing=print
echo

echo "# Git checkout fails without internet"
mv ../server_repo ../server_repo.off
! git checkout master
echo

echo "# Git checkout fetches the missing directory from internet"
mv ../server_repo.off ../server_repo
git checkout master -- d1/
echo

echo "# Missing objects after checking out d1"
git rev-list --all --quiet --objects --missing=print
Run Code Online (Sandbox Code Playgroud)

GitHub 上游.

Git v2.19.0 中的输出:

# List and identify all objects
c6fcdfaf2b1462f809aecdad83a186eeec00f9c1
fc5e97944480982cfc180a6d6634699921ee63ec
7251a83be9a03161acde7b71a8fda9be19f47128
62d67bce3c672fe2b9065f372726a11e57bade7e
b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
308150e8fddde043f3dbbb8573abb6af1df96e63 d1/a
f70a17f51b7b30fec48a32e4f19ac15e261fd1a4 d1/b
84de03c312dc741d0f2a66df7b2f168d823e122a d2
0975df9b39e23c15f63db194df7f45c76528bccb d2/a
41484c13520fcbb6e7243a26fdb1fc9405c08520 d2/b
7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
8b25206ff90e9432f6f1a8600f87a7bd695a24af master/master
ef29f15c9a7c5417944cc09711b6a9ee51b01d89
19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
1b671b190e293aa091239b8b5e8c149411d00523 mybranch/mybranch
c3760bb1a0ece87cdbaf9a563c77a45e30a4e30e
a0234da53ec608b54813b4271fbf00ba5318b99f root
93ca1422a8da0a9effc465eccbcb17e23015542d root/root
master commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
mybranch commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
040000 tree b64bf435a3e54c5208a1b70b7bcb0fc627463a75    d1
040000 tree 84de03c312dc741d0f2a66df7b2f168d823e122a    d2
040000 tree 7d5230379e4652f1b1da7ed1e78e0b8253e03ba3    master
040000 tree 19f7a4ca4a038aff89d803f017f76d2b66063043    mybranch
040000 tree a0234da53ec608b54813b4271fbf00ba5318b99f    root

# Missing objects after --no-checkout
?f70a17f51b7b30fec48a32e4f19ac15e261fd1a4
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
?308150e8fddde043f3dbbb8573abb6af1df96e63

# Git checkout fails without internet
fatal: '/home/ciro/bak/git/test-git-web-interface/other-test-repos/partial-clone.tmp/server_repo' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

# Git checkout fetches the missing directory from internet
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.

# Missing objects after checking out d1
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
Run Code Online (Sandbox Code Playgroud)

结论:所有来自外部的 blobd1/都丢失了。例如0975df9b39e23c15f63db194df7f45c76528bccbd2/b退房后就没有了d1/a

请注意,root/rootmybranch/mybranch也丢失,但--depth 1将其从丢失文件列表中隐藏。如果您删除--depth 1,则它们会显示在丢失文件列表中。

我有一个梦想

这个特性可以彻底改变 Git。

想象一下,没有丑陋的第三方工具(如repo.

想象一下,在没有任何丑陋的第三方扩展的情况下直接在 repo 中存储巨大的 blob

想象一下,如果 GitHub 允许每个文件/目录的元数据,例如星星和权限,那么您可以将所有个人资料存储在一个存储库中。

想象一下,如果子模块被完全像常规目录一样对待:只需请求一个树 SHA,一个类似 DNS 的机制会解析您的请求,首先查看您的本地~/.git,然后首先查看更近的服务器(您企业的镜像/缓存),最后在 GitHub 上结束。


小智 10

尝试这个:

svn export https://github.com/PhantomX/slackbuilds/trunk/${directory}
Run Code Online (Sandbox Code Playgroud)