如何只从 git 存储库中克隆一些目录?

sam*_*sam 35 git

例如,我想下载 PCL 3d_rec_framework。

这是PCL的git仓库:https : //github.com/PointCloudLibrary/pcl.git

怎么下载这个目录?

https://github.com/PointCloudLibrary/pcl/tree/master/apps

我试过这个,但没有用:

sam@sam:~/code/pcl_standalone$ git clone https://github.com/PointCloudLibrary/pcl/tree/master/apps/3d_rec_framework
Cloning into '3d_rec_framework'...
error: The requested URL returned error: 403 while accessing https://github.com/PointCloudLibrary/pcl/tree/master/apps/3d_rec_framework/info/refs
fatal: HTTP request failed
sam@sam:~/code/pcl_standalone$ 
Run Code Online (Sandbox Code Playgroud)

我不想下载 PCL 的 git 并删除我不想要的所有其他目录。

如何只下载一个目录?

小智 54

自 git v1.7 以来,dobey 的答案不再如此。您现在可以从存储库中检出某些文件夹。完整说明可在此处找到。

git init <repo>
cd <repo>
git remote add -f origin <url>

git config core.sparseCheckout true

echo "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout
Run Code Online (Sandbox Code Playgroud)

这会告诉 git 您要检出哪些目录。然后你可以只拉那些目录

git pull origin master
Run Code Online (Sandbox Code Playgroud)

  • 这仍然会克隆整个存储库,然后进行稀疏结帐。 (5认同)
  • 这意味着所有 Ubuntu 版本都有 1.7 可用。您应该检查情况是否如此,并在此处评论您的答案,以了解哪些个别版本实际上可以工作。在我看来,PowerShell 也不是 Ubuntu,因此不应包括在内。 (3认同)
  • @ThomasW。所有当前支持的 Ubuntu 版本都至少包含 git 1.7,现在大多数是 2.x。 (2认同)
  • @dobey,说真的,你删除了人们在谷歌上找到这个问题可能非常寻找的有用信息?!如果我被迫使用 powershell,我肯定想查看管道详细信息,它们并不明显!回显“某些/目录/”| Out-File -Encoding ascii .git/info/sparse-checkout echo "another/sub/tree/" | Out-File -Append -Encoding ascii .git/info/sparse-checkout (2认同)

Cel*_*ser 10

首先,做:

git clone --depth 1 [repo root] [name of destination directory]
Run Code Online (Sandbox Code Playgroud)

然后:

cd [name of destination directory]
Run Code Online (Sandbox Code Playgroud)

...最后:

git filter-branch --prune-empty --subdirectory-filter [path to sub-dir] HEAD
Run Code Online (Sandbox Code Playgroud)

就这么简单。Git 将重写 repo,以便只包含所需的子目录。即使子目录有几层深,这也有效。只需将目标目录命名为子目录的名称。然后在“git filter-branch”命令中放置子目录的相对路径。哦,--depth 1告诉git只下载头顶(基本上删除历史记录)。

  • 有没有一种简单的方法可以不时刷新该目录? (2认同)

Cir*_*郝海东 8

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 上结束。


dob*_*bey 7

你不能。使用 git,您可以克隆整个存储库以及存储库的完整历史记录。

有一些解决方案可以从 git 存档中获取单个文件,列在Stack Exchange 对同一问题的回答中,但您仍然需要下载整个存储库才能获取所需的单个文件或目录。

  • 根本不是真的:http://askubuntu.com/a/729798/384425 (7认同)
  • @CelticParser 所以你声称我的答案不正确,然后继续指出一个需要从 git 存储库下载每个文件以获得单个文件的答案? (5认同)
  • 或 http://askubuntu.com/a/645276/384425 (2认同)