我正在使用一个包含大量文件的存储库,需要几个小时才能签出。我正在研究 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?
发布评论
评论(16)
请注意,此答案确实从存储库下载了数据的完整副本。 git remote add -f 命令将克隆整个存储库。来自
git-remote
的手册页:试试这个:
现在您会发现您有一个“修剪”的签出,仅存在来自 path/within_repo/to/desired_subdir 的文件(并且在该路径中)。
请注意,在 Windows 命令行上,您不能引用路径,即您必须用以下命令更改第 6 个命令:
如果不这样做,您将在稀疏结帐中获得引号文件,它不会工作
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 ofgit-remote
:Try this:
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:
if you don't you'll get the quotes in the sparse-checkout file, and it will not work
2020 年,有一种更简单的方法来处理稀疏签出,而不必担心 .git 文件。我是这样做的:
请注意,它需要安装 git 版本 2.25。在这里阅读更多相关信息: https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/
更新:
上面的
git clone
命令仍然会克隆存储库及其完整历史记录,但不会签出文件。如果您不需要完整的历史记录,可以添加 --深度 参数,如下所示: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:
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:适用于 git v2.37.1+
指定要克隆的文件夹
Works in git v2.37.1+
Specify the folders you want to clone
Git clone 有一个选项(
--no-checkout
或-n
)可以完成您想要的操作。在命令列表中,只需更改:
对此:
然后您可以使用问题中所述的稀疏结帐。
Git clone has an option (
--no-checkout
or-n
) that does what you want.In your list of commands, just change:
To this:
You can then use the sparse checkout as stated in the question.
我有一个类似的用例,但我只想签出标签的提交并修剪目录。使用
--depth 1
使其变得非常稀疏,并且可以真正加快速度。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.我从 pavek 之前发布的单行文字中找到了我正在寻找的答案(谢谢!),所以我想在一个适用于 Linux (GIT 1.7.1) 的回复中提供完整的答案:
我稍微改变了命令的顺序,但这似乎没有任何影响。关键是步骤 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):
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.
更新答案2020:
现在有一个命令
git稀疏-checkout
,我详细介绍了 Git 2.25(2020 年第一季度)nicono 的 答案 说明了其用法:
它有 < 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 日)对于 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 日)原始答案: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
手册页 现在包括: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:
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 repositoryNote/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)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)Original answer: 2016
git 2.9 (June 2016) will generalize the
--no-checkout
option togit 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:遗憾的是,上述方法都不适合我,因此我花了很长时间尝试
sparse-checkout
文件的不同组合。就我而言,我想跳过具有 IntelliJ IDEA 配置的文件夹。
这是我所做的:
运行
git clone https://github.com/myaccount/myrepo.git --no-checkout
运行
git config core.sparsecheckout true
创建 < code>.git\info\sparse-checkout 包含以下内容
运行“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 contentRun 'git checkout --' to get all files.
Critical thing to make it work was to add
/*
after folder's name.I have git 1.9
基于这个答案,作者:apenwarr 和 此评论,作者:Miral 我想出了以下解决方案,节省了近 94% 的磁盘空间在本地克隆 linux git 存储库时只需要一个文档子目录时需要占用空间:
所以我从 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:
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 ofgit clone --filter=blob:none
forgit fetch
, we can probably save some more megabytes. (Reading the man page ofgit-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.)
是的,可以下载一个文件夹而不是下载整个存储库。即使任何/最后一次提交
这样做的好方法
-r HEAD 也只会下载最后的修订版本,忽略所有历史记录。
注意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
-r HEAD will only download last revision, ignore all history.
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
仅稀疏签出特定文件夹的步骤:
Steps to sparse checkout only specific folder:
在 git 2.27 中,git稀疏结帐看起来已经进化了。
this 答案中的解决方案的工作方式并不完全相同(与 git 2.25 相比)
这些命令效果更好:
另请参阅:git-clone --sparse 和 git-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)
These commands worked better:
See also : git-clone --sparse and git-sparse-checkout add
我是 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?
我从 TypeScript 定义库 @types 中获取此内容:
假设存储库具有以下结构:
您的目标:仅签出身份/文件夹。及其所有内容,包括子文件夹。
⚠️ 这需要最低 git 版本 2.27.0,这可能比大多数计算机上的默认版本更新。旧版本中提供了更复杂的过程,但本指南未涵盖。
这会将 types/identity 文件夹检出到本地计算机。
--sparse
初始化稀疏检出文件,以便工作目录仅以根目录中的文件开头存储库。--filter=blob:none
将排除文件,仅根据需要获取它们。--depth=1
将通过截断提交历史记录进一步提高克隆速度,但可能会导致问题 此处。I took this from TypeScript definitions library @types:
Let's say the repo has this structure:
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.
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.就我而言,我想在克隆项目时跳过
Pods
文件夹。我按照下面的步骤做了,它对我有用。希望有帮助。
备注,如果您想跳过更多文件夹,只需在稀疏结帐文件中添加更多行即可。
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.
Memo, If you want to skip more folders, just add more line in sparse-checkout file.
接受的答案没有完全按照我的需要工作,因为它还从根文件夹下载文件。
这是我只下载一个我需要的文件夹的方法:
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: