是否可以在不先检出整个存储库的情况下进行稀疏检出?

发布于 2024-09-30 18:38:42 字数 417 浏览 3 评论 0 原文

我正在使用一个包含大量文件的存储库,需要几个小时才能签出。我正在研究 Git 是否可以很好地与这种存储库配合使用,因为它支持稀疏签出,但我能找到的每个示例都执行以下操作:

git clone <path>
git config core.sparsecheckout true
echo <dir> > .git/info/sparse-checkout
git read-tree -m -u HEAD

此命令序列的问题是原始克隆也执行签出。如果将 -n 添加到原始克隆命令中,则 read-tree 命令会导致以下错误:

error: Sparse checkout leaves no Entry onworking Directory

如何在不先检出所有文件的情况下执行稀疏检出?

I'm working with a repository with a very large number of files that takes hours to checkout. I'm looking into the possibility of whether Git would work well with this kind of repository now that it supports sparse checkouts but every example that I can find does the following:

git clone <path>
git config core.sparsecheckout true
echo <dir> > .git/info/sparse-checkout
git read-tree -m -u HEAD

The problem with this sequence of commands is the original clone also does a checkout. If you add -n to the original clone command, then the read-tree command results in the following error:

error: Sparse checkout leaves no entry on working directory

How can do the sparse checkout without checking out all the files first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

罪#恶を代价 2024-10-07 18:38:42

请注意,此答案确实从存储库下载了数据的完整副本。 git remote add -f 命令将克隆整个存储库。来自 git-remote 的手册页

使用-f选项,git fetch 在远程信息设置后立即运行。


试试这个:

mkdir myrepo
cd myrepo
git init
git config core.sparseCheckout true
git remote add -f origin git://...
echo "path/within_repo/to/desired_subdir/*" > .git/info/sparse-checkout
git checkout [branchname] # ex: master

现在您会发现您有一个“修剪”的签出,仅存在来自 path/within_repo/to/desired_subdir 的文件(并且在该路径中)。

请注意,在 Windows 命令行上,您不能引用路径,即您必须用以下命令更改第 6 个命令:

echo path/within_repo/to/desired_subdir/* > .git/info/sparse-checkout

如果不这样做,您将在稀疏结帐中获得引号文件,它不会工作

Please note that this answer does download a complete copy of the data from a repository. The git remote add -f command will clone the whole repository. From the man page of git-remote:

With -f option, git fetch <name> is run immediately after the remote information is set up.


Try this:

mkdir myrepo
cd myrepo
git init
git config core.sparseCheckout true
git remote add -f origin git://...
echo "path/within_repo/to/desired_subdir/*" > .git/info/sparse-checkout
git checkout [branchname] # ex: master

Now you will find that you have a "pruned" checkout with only files from path/within_repo/to/desired_subdir present (and in that path).

Note that on windows command line you must not quote the path, i.e. you must change the 6th command with this one:

echo path/within_repo/to/desired_subdir/* > .git/info/sparse-checkout

if you don't you'll get the quotes in the sparse-checkout file, and it will not work

荭秂 2024-10-07 18:38:42

2020 年,有一种更简单的方法来处理稀疏签出,而不必担心 .git 文件。我是这样做的:

git clone <URL> --no-checkout <directory>
cd <directory>
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout set apps/my_app libs/my_lib # etc, to list sub-folders to checkout
git checkout # or git switch

请注意,它需要安装 git 版本 2.25。在这里阅读更多相关信息: https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/

更新:

上面的git clone命令仍然会克隆存储库及其完整历史记录,但不会签出文件。如果您不需要完整的历史记录,可以添加 --深度 参数,如下所示:

# create a shallow clone,
# with only 1 (since depth equals 1) latest commit in history
git clone <URL> --no-checkout <directory> --depth 1

In 2020 there is a simpler way to deal with sparse-checkout without having to worry about .git files. Here is how I did it:

git clone <URL> --no-checkout <directory>
cd <directory>
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout set apps/my_app libs/my_lib # etc, to list sub-folders to checkout
git checkout # or git switch

Note that it requires git version 2.25 installed. Read more about it here: https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/

UPDATE:

The above git clone command will still clone the repo with its full history, though without checking the files out. If you don't need the full history, you can add --depth parameter to the command, like this:

# create a shallow clone,
# with only 1 (since depth equals 1) latest commit in history
git clone <URL> --no-checkout <directory> --depth 1
可遇━不可求 2024-10-07 18:38:42

适用于 git v2.37.1+

git clone --filter=blob:none --no-checkout --depth 1 --sparse <project-url>
cd <project>

指定要克隆的文件夹

git sparse-checkout add <folder1> <folder2>
git checkout

Works in git v2.37.1+

git clone --filter=blob:none --no-checkout --depth 1 --sparse <project-url>
cd <project>

Specify the folders you want to clone

git sparse-checkout add <folder1> <folder2>
git checkout
荒芜了季节 2024-10-07 18:38:42

Git clone 有一个选项(--no-checkout-n)可以完成您想要的操作。

在命令列表中,只需更改:

git clone <path>

对此:

git clone --no-checkout <path>

然后您可以使用问题中所述的稀疏结帐。

Git clone has an option (--no-checkout or -n) that does what you want.

In your list of commands, just change:

git clone <path>

To this:

git clone --no-checkout <path>

You can then use the sparse checkout as stated in the question.

一百个冬季 2024-10-07 18:38:42

我有一个类似的用例,但我只想签出标签的提交并修剪目录。使用 --depth 1 使其变得非常稀疏,并且可以真正加快速度。

mkdir myrepo
cd myrepo
git init
git config core.sparseCheckout true
git remote add origin <url>  # Note: no -f option
echo "path/within_repo/to/subdir/" > .git/info/sparse-checkout
git fetch --depth 1 origin tag <tagname>
git checkout <tagname>

I had a similar use case, except I wanted to checkout only the commit for a tag and prune the directories. Using --depth 1 makes it really sparse and can really speed things up.

mkdir myrepo
cd myrepo
git init
git config core.sparseCheckout true
git remote add origin <url>  # Note: no -f option
echo "path/within_repo/to/subdir/" > .git/info/sparse-checkout
git fetch --depth 1 origin tag <tagname>
git checkout <tagname>
叹倦 2024-10-07 18:38:42

我从 pavek 之前发布的单行文字中找到了我正在寻找的答案(谢谢!),所以我想在一个适用于 Linux (GIT 1.7.1) 的回复中提供完整的答案:

1--> mkdir myrepo
2--> cd myrepo
3--> git init
4--> git config core.sparseCheckout true
5--> echo 'path/to/subdir/' > .git/info/sparse-checkout
6--> git remote add -f origin ssh://...
7--> git pull origin master

我稍微改变了命令的顺序,但这似乎没有任何影响。关键是步骤 5 中路径末尾处存在尾部斜杠“/”。

I found the answer I was looking for from the one-liner posted earlier by pavek (thanks!) so I wanted to provide a complete answer in a single reply that works on Linux (GIT 1.7.1):

1--> mkdir myrepo
2--> cd myrepo
3--> git init
4--> git config core.sparseCheckout true
5--> echo 'path/to/subdir/' > .git/info/sparse-checkout
6--> git remote add -f origin ssh://...
7--> git pull origin master

I changed the order of the commands a bit but that does not seem to have any impact. The key is the presence of the trailing slash "/" at the end of the path in step 5.

放飞的风筝 2024-10-07 18:38:42

更新答案2020:

现在有一个命令 git稀疏-checkout ,我详细介绍了 Git 2.25(2020 年第一季度)

nicono答案 说明了其用法:

git sparse-checkout init --cone # to fetch only root files
git sparse-checkout add apps/my_app
git sparse-checkout add libs/my_lib

它有 < a href="https://stackoverflow.com/a/59515444/6309">随 Git 2.27 一起发展并且知道如何“重新应用”稀疏结帐,如此处
请注意,在 Git 2.28 中,git status 会提到您位于稀疏签出存储库中


注意/警告:某些在非圆锥模式下有效的稀疏签出模式会导致圆锥模式下出现段错误,该问题已通过 Git 2.35(2022 年第一季度)得到纠正。

请参阅提交a3eca58提交 391c3a1提交a481d43(2021 年 12 月 16 日),作者:Derrick Stolee (derrickstolee)
(由 Junio C Hamano -- gitster -- 合并于 提交 09481fe,2022 年 1 月 10 日)

sparse-checkout:拒绝添加到不良模式

审阅者:Elijah Newren
签字人:Derrick Stolee

当处于圆锥模式稀疏结帐时,不清楚 'git稀疏结帐'(man) add ... 如果现有的稀疏检出文件与锥体模式模式不匹配,则应执行操作。
将行为更改为失败,并显示有关现有模式的错误消息。

此外,所有圆锥模式模式均以“/”字符开头,因此请添加该限制。
这对于我们的示例测试“圆锥模式:错误模式发出警告”是必要的,但也需要修改我们用来测试与识别圆锥模式模式相关的警告的示例稀疏检出文件。

此错误检查将导致测试脚本进一步失败,因为测试添加了非圆锥模式模式而不清理它们。
现在将清理作为测试的一部分执行。


对于 Git 2.36(2022 年第 2 季度),“ git稀疏结帐(man) 希望使用每个工作树配置,但在附加到裸存储库的工作树中效果不佳。

请参阅 提交 3ce1138提交 5325591提交7316dc5提交 fe18733提交 615a84a提交 5c11c0d(2022 年 2 月 7 日),作者:Derrick Stolee (derrickstolee)
(由 Junio C Hamano -- gitster -- 合并于 提交 6249ce2,2022 年 2 月 25 日)

worktree:复制稀疏结帐模式并添加配置

签字人:Derrick Stolee
审阅者:Elijah Newren

添加新工作树时,我们有理由期望为该新工作树使用当前的稀疏检出设置集。
这对于工作树变得太大而无法使用的存储库尤其重要。
当使用部分克隆时,这一点甚至更为重要,因为我们希望避免下载不应写入新工作树的文件的丢失 blob。

创建这样一个工作树而不需要扩展整个工作树的中间步骤的唯一方法是在“git worktree add'(man)
每个工作树都有自己的稀疏检出模式,稀疏检出文件丢失时的默认行为是包含 HEAD 处的所有路径。
因此,我们需要从某个地方获取模式,它们也可能是当前工作树的模式。
这些然后在将来独立修改。

除了稀疏签出文件之外,如果启用了工作树配置并且该文件存在,请复制工作树配置文件。
这将复制所有重要设置,以确保新工作树的行为与当前工作树相同。
我们必须继续做的唯一例外是 core.barecore.worktree 应该在工作树的配置文件中取消设置。


原始答案:2016

git 2.9(2016 年 6 月)将把 --no-checkout 选项推广到 git worktree add (该命令允许与 一个仓库有多个工作树

请参阅提交 ef2a0ac(2016 年 3 月 29 日),作者:Ray 张 (OneRaynyDay) )
帮助者:Eric Sunshine (sunshineco)Junio C Hamano (gitster)
(由 Junio C Hamano -- gitster -- 合并于 提交 0d8683c,2016 年 4 月 13 日)

git worktree 手册页 现在包括:

--[no-]checkout:

默认情况下,add 会签出 ,但是,--no-checkout 可用于按顺序抑制签出进行自定义,例如配置稀疏结帐

Updated answer 2020:

There is now a command git sparse-checkout, that I present in detail with Git 2.25 (Q1 2020)

nicono's answer illustrates its usage:

git sparse-checkout init --cone # to fetch only root files
git sparse-checkout add apps/my_app
git sparse-checkout add libs/my_lib

It has evolved with Git 2.27 and knows how to "reapply" a sparse checkout, as in here.
Note that with Git 2.28, git status will mention that you are in a sparse-checked-out repository


Note/Warning: Certain sparse-checkout patterns that are valid in non-cone mode led to segfault in cone mode, which has been corrected with Git 2.35 (Q1 2022).

See commit a3eca58, commit 391c3a1, commit a481d43 (16 Dec 2021) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster -- in commit 09481fe, 10 Jan 2022)

sparse-checkout: refuse to add to bad patterns

Reviewed-by: Elijah Newren
Signed-off-by: Derrick Stolee

When in cone mode sparse-checkout, it is unclear how 'git sparse-checkout'(man) add ... should behave if the existing sparse-checkout file does not match the cone mode patterns.
Change the behavior to fail with an error message about the existing patterns.

Also, all cone mode patterns start with a '/' character, so add that restriction.
This is necessary for our example test 'cone mode: warn on bad pattern', but also requires modifying the example sparse-checkout file we use to test the warnings related to recognizing cone mode patterns.

This error checking would cause a failure further down the test script because of a test that adds non-cone mode patterns without cleaning them up.
Perform that cleanup as part of the test now.


With Git 2.36 (Q2 2022), "git sparse-checkout"(man) wants to work with per-worktree configuration, but did not work well in a worktree attached to a bare repository.

See commit 3ce1138, commit 5325591, commit 7316dc5, commit fe18733, commit 615a84a, commit 5c11c0d (07 Feb 2022) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster -- in commit 6249ce2, 25 Feb 2022)

worktree: copy sparse-checkout patterns and config on add

Signed-off-by: Derrick Stolee
Reviewed-by: Elijah Newren

When adding a new worktree, it is reasonable to expect that we want to use the current set of sparse-checkout settings for that new worktree.
This is particularly important for repositories where the worktree would become too large to be useful.
This is even more important when using partial clone as well, since we want to avoid downloading the missing blobs for files that should not be written to the new worktree.

The only way to create such a worktree without this intermediate step of expanding the full worktree is to copy the sparse-checkout patterns and config settings during 'git worktree add'(man).
Each worktree has its own sparse-checkout patterns, and the default behavior when the sparse-checkout file is missing is to include all paths at HEAD.
Thus, we need to have patterns from somewhere, they might as well be the current worktree's patterns.
These are then modified independently in the future.

In addition to the sparse-checkout file, copy the worktree config file if worktree config is enabled and the file exists.
This will copy over any important settings to ensure the new worktree behaves the same as the current one.
The only exception we must continue to make is that core.bare and core.worktree should become unset in the worktree's config file.


Original answer: 2016

git 2.9 (June 2016) will generalize the --no-checkout option to git worktree add (the command which allows to works with multiple working trees for one repo)

See commit ef2a0ac (29 Mar 2016) by Ray Zhang (OneRaynyDay).
Helped-by: Eric Sunshine (sunshineco), and Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 0d8683c, 13 Apr 2016)

The git worktree man page now includes:

--[no-]checkout:

By default, add checks out <branch>, however, --no-checkout can be used to suppress checkout in order to make customizations, such as configuring sparse-checkout.

深爱成瘾 2024-10-07 18:38:42

遗憾的是,上述方法都不适合我,因此我花了很长时间尝试 sparse-checkout 文件的不同组合。

就我而言,我想跳过具有 IntelliJ IDEA 配置的文件夹。

这是我所做的:


运行 git clone https://github.com/myaccount/myrepo.git --no-checkout

运行 git config core.sparsecheckout true

创建 < code>.git\info\sparse-checkout 包含以下内容

!.idea/*
!.idea_modules/*
/*

运行“git checkout --”以获取所有文件。


使其工作的关键是在文件夹名称后添加 /*

我有 git 1.9

Sadly none of the above worked for me so I spent very long time trying different combination of sparse-checkout file.

In my case I wanted to skip folders with IntelliJ IDEA configs.

Here is what I did:


Run git clone https://github.com/myaccount/myrepo.git --no-checkout

Run git config core.sparsecheckout true

Created .git\info\sparse-checkout with following content

!.idea/*
!.idea_modules/*
/*

Run 'git checkout --' to get all files.


Critical thing to make it work was to add /* after folder's name.

I have git 1.9

ぃ弥猫深巷。 2024-10-07 18:38:42

基于这个答案,作者:apenwarr 此评论,作者:Miral 我想出了以下解决方案,节省了近 94% 的磁盘空间在本地克隆 linux git 存储库时只需要一个文档子目录时需要占用空间:

$ cd linux
$ du -sh .git .
2.1G    .git
894M    .
$ du -sh 
2.9G    .
$ mkdir ../linux-sparse-test
$ cd ../linux-sparse-test
$ git init
Initialized empty Git repository in /…/linux-sparse-test/.git/
$ git config core.sparseCheckout true
$ git remote add origin ../linux
# Parameter "origin master" saves a tiny bit if there are other branches
$ git fetch --depth=1 origin master
remote: Enumerating objects: 65839, done.
remote: Counting objects: 100% (65839/65839), done.
remote: Compressing objects: 100% (61140/61140), done.
remote: Total 65839 (delta 6202), reused 22590 (delta 3703)
Receiving objects: 100% (65839/65839), 173.09 MiB | 10.05 MiB/s, done.
Resolving deltas: 100% (6202/6202), done.
From ../linux
 * branch              master     -> FETCH_HEAD
 * [new branch]        master     -> origin/master
$ echo "Documentation/hid/*" > .git/info/sparse-checkout
$ git checkout master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Already on 'master'
$ ls -l
total 4
drwxr-xr-x 3 abe abe 4096 May  3 14:12 Documentation/
$  du -sh .git .
181M    .git
100K    .
$  du -sh
182M    .

所以我从 2.9GB 减少到 182MB,这已经很不错了。

我虽然没有让它与 git clone --depth 1 --no-checkout --filter=blob:none file:///.../linux linux-sparse-test (< a href="https://unix.stackexchange.com/a/468182/94426">此处提示),然后丢失的文件全部作为已删除的文件添加到索引中。因此,如果有人知道 git fetch 的 git clone --filter=blob:none 等价物,我们可能可以节省更多兆字节。 (阅读 git-rev-list 的手册页也暗示有类似 --filter=sparse:path=... 的内容,但我没有明白这一点(

都尝试使用 Debian Buster 的 git 2.20.1。)

Based on this answer by apenwarr and this comment by Miral I came up with the following solution which saved me nearly 94% of disk space when cloning the linux git repository locally while only wanting one Documentation subdirectory:

$ cd linux
$ du -sh .git .
2.1G    .git
894M    .
$ du -sh 
2.9G    .
$ mkdir ../linux-sparse-test
$ cd ../linux-sparse-test
$ git init
Initialized empty Git repository in /…/linux-sparse-test/.git/
$ git config core.sparseCheckout true
$ git remote add origin ../linux
# Parameter "origin master" saves a tiny bit if there are other branches
$ git fetch --depth=1 origin master
remote: Enumerating objects: 65839, done.
remote: Counting objects: 100% (65839/65839), done.
remote: Compressing objects: 100% (61140/61140), done.
remote: Total 65839 (delta 6202), reused 22590 (delta 3703)
Receiving objects: 100% (65839/65839), 173.09 MiB | 10.05 MiB/s, done.
Resolving deltas: 100% (6202/6202), done.
From ../linux
 * branch              master     -> FETCH_HEAD
 * [new branch]        master     -> origin/master
$ echo "Documentation/hid/*" > .git/info/sparse-checkout
$ git checkout master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Already on 'master'
$ ls -l
total 4
drwxr-xr-x 3 abe abe 4096 May  3 14:12 Documentation/
$  du -sh .git .
181M    .git
100K    .
$  du -sh
182M    .

So I got down from 2.9GB to 182MB which is already quiet nice.

I though didn't get this to work with git clone --depth 1 --no-checkout --filter=blob:none file:///…/linux linux-sparse-test (hinted here) as then the missing files were all added as removed files to the index. So if anyone knows the equivalent of git clone --filter=blob:none for git fetch, we can probably save some more megabytes. (Reading the man page of git-rev-list also hints that there is something like --filter=sparse:path=…, but I didn't get that to work either.

(All tried with git 2.20.1 from Debian Buster.)

ゞ记忆︶ㄣ 2024-10-07 18:38:42

是的,可以下载一个文件夹而不是下载整个存储库。即使任何/最后一次提交

这样做的好方法

D:\Lab>git svn clone https://github.com/Qamar4P/LolAdapter.git/trunk/lol-adapter -r HEAD
  1. -r HEAD 也只会下载最后的修订版本,忽略所有历史记录。

  2. 注意trunk和/specific-folder

/trunk/.我希望这会对某人有所帮助。尽情享受吧:)

更新于 2019 年 9 月 26 日

Yes, Possible to download a folder instead of downloading the whole repository. Even any/last commit

Nice way to do this

D:\Lab>git svn clone https://github.com/Qamar4P/LolAdapter.git/trunk/lol-adapter -r HEAD
  1. -r HEAD will only download last revision, ignore all history.

  2. Note trunk and /specific-folder

Copy and change URL before and after /trunk/. I hope this will help someone. Enjoy :)

Updated on 26 Sep 2019

巨坚强 2024-10-07 18:38:42

仅稀疏签出特定文件夹的步骤:

1) git clone --no-checkout  <project clone url>  
2) cd <project folder>
3) git config core.sparsecheckout true   [You must do this]
4) echo "<path you want to sparce>/*" > .git/info/sparse-checkout
    [You must enter /* at the end of the path such that it will take all contents of that folder]
5) git checkout <branch name> [Ex: master]

Steps to sparse checkout only specific folder:

1) git clone --no-checkout  <project clone url>  
2) cd <project folder>
3) git config core.sparsecheckout true   [You must do this]
4) echo "<path you want to sparce>/*" > .git/info/sparse-checkout
    [You must enter /* at the end of the path such that it will take all contents of that folder]
5) git checkout <branch name> [Ex: master]
牵你的手,一向走下去 2024-10-07 18:38:42

在 git 2.27 中,git稀疏结帐看起来已经进化了。
this 答案中的解决方案的工作方式并不完全相同(与 git 2.25 相比)

git clone ; --no-checkout <目录>;
cd <目录>
git稀疏-checkout init --cone # 仅获取根文件
git稀疏-checkout set apps/my_app libs/my_lib #等,列出要签出的子文件夹
# 此命令后将立即检出它们,无需运行 git pull

这些命令效果更好:

git clone --sparse <URL> <directory>
cd <directory>
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout add apps/my_app
git sparse-checkout add libs/my_lib

另请参阅:git-clone --sparsegit-sparse-checkout 添加

In git 2.27, it looks like git sparse checkout has evolved.
Solution in this answer does not work exactly the same way (compared to git 2.25)

git clone <URL> --no-checkout <directory>
cd <directory>
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout set apps/my_app libs/my_lib # etc, to list sub-folders to checkout
# they are checked out immediately after this command, no need to run git pull

These commands worked better:

git clone --sparse <URL> <directory>
cd <directory>
git sparse-checkout init --cone # to fetch only root files
git sparse-checkout add apps/my_app
git sparse-checkout add libs/my_lib

See also : git-clone --sparse and git-sparse-checkout add

鹿港小镇 2024-10-07 18:38:42

我是 git 新手,但似乎如果我对每个目录执行 git checkout 那么它就可以工作。此外,稀疏签出文件需要在每个目录后有一个尾部斜杠,如所示。有更多经验的人请确认这是否有效。

有趣的是,如果您签出不在稀疏签出文件中的目录,则似乎没有什么区别。它们不会出现在 git status 中,并且 git read-tree -m -u HEAD 不会导致它被删除。 git reset --hard 也不会导致目录被删除。任何更有经验的人都愿意评论 git 对已签出但不在稀疏签出文件中的目录的看法吗?

I'm new to git but it seems that if I do git checkout for each directory then it works. Also, the sparse-checkout file needs to have a trailing slash after every directory as indicated. Someone more experience please confirm that this will work.

Interestingly, if you checkout a directory not in the sparse-checkout file it seems to make no difference. They don't show up in git status and git read-tree -m -u HEAD doesn't cause it to be removed. git reset --hard doesn't cause the directory to be removed either. Anyone more experienced care to comment on what git thinks of directories that are checked out but which are not in the sparse checkout file?

忆沫 2024-10-07 18:38:42

我从 TypeScript 定义库 @types 中获取此内容:

假设存储库具有以下结构:

types/
|_ identity/
|_ etc...

您的目标:仅签出身份/文件夹。及其所有内容,包括子文件夹。

⚠️ 这需要最低 git 版本 2.27.0,这可能比大多数计算机上的默认版本更新。旧版本中提供了更复杂的过程,但本指南未涵盖。

git clone --sparse --filter=blob:none --depth=1 <source-repo-url>
git sparse-checkout add types/identity types/identity ...

这会将 types/identity 文件夹检出到本地计算机。

--sparse 初始化稀疏检出文件,以便工作目录仅以根目录中的文件开头存储库。

--filter=blob:none 将排除文件,仅根据需要获取它们。

--depth=1 将通过截断提交历史记录进一步提高克隆速度,但可能会导致问题 此处

I took this from TypeScript definitions library @types:

Let's say the repo has this structure:

types/
|_ identity/
|_ etc...

Your goal: Checkout identity/ folder ONLY. With all its contents including subfolders.

⚠️ This requires minimum git version 2.27.0, which is likely newer than the default on most machines. More complicated procedures are available in older versions, but not covered by this guide.

git clone --sparse --filter=blob:none --depth=1 <source-repo-url>
git sparse-checkout add types/identity types/identity ...

This will check out the types/identity folder to your local machine.

--sparse initializes the sparse-checkout file so the working directory starts with only the files in the root of the repository.

--filter=blob:none will exclude files, fetching them only as needed.

--depth=1 will further improve clone speed by truncating commit history, but it may cause issues as summarized here.

深空失忆 2024-10-07 18:38:42

就我而言,我想在克隆项目时跳过 Pods 文件夹。我按照下面的步骤做了,它对我有用。
希望有帮助。

mkdir my_folder
cd my_folder
git init
git remote add origin -f <URL>
git config core.sparseCheckout true 
echo '!Pods/*\n/*' > .git/info/sparse-checkout
git pull origin master

备注,如果您想跳过更多文件夹,只需在稀疏结帐文件中添加更多行即可。

In my case, I want to skip the Pods folder when cloning the project. I did step by step like below and it works for me.
Hope it helps.

mkdir my_folder
cd my_folder
git init
git remote add origin -f <URL>
git config core.sparseCheckout true 
echo '!Pods/*\n/*' > .git/info/sparse-checkout
git pull origin master

Memo, If you want to skip more folders, just add more line in sparse-checkout file.

想你只要分分秒秒 2024-10-07 18:38:42

接受的答案没有完全按照我的需要工作,因为它还从根文件夹下载文件。

这是我只下载一个我需要的文件夹的方法:

git clone --no-checkout --depth=1 --filter=blob:none --branch=$(VERSION) $(REPO_URL) && \
cd $(REPO_NAME) && \
git config core.sparseCheckout true && \
echo "$(FOLDER)/" >> .git/info/sparse-checkout && \
git checkout

Accepted answer not fully worked as needed for me, because it also downloads files from root folder.

This is how I downloaded only one folder that I needed:

git clone --no-checkout --depth=1 --filter=blob:none --branch=$(VERSION) $(REPO_URL) && \
cd $(REPO_NAME) && \
git config core.sparseCheckout true && \
echo "$(FOLDER)/" >> .git/info/sparse-checkout && \
git checkout
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文