我正在使用一个存储库,其中有大量的文件,需要几个小时才能签出。我正在研究Git是否能很好地使用这种存储库的可能性,因为它支持稀疏签出,但我能找到的每个例子都是这样的:

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

这个命令序列的问题是原来的克隆也做一个签出。如果在原来的clone命令中添加了-n,则read-tree命令将导致以下错误:

错误:稀疏签出在工作目录上没有留下条目

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


当前回答

根据apenwarr的回答和Miral的评论,我提出了以下解决方案,在本地克隆linux git存储库时节省了近94%的磁盘空间,同时只需要一个文档子目录:

$ 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克隆-深度1 -no-checkout -filter=blob:none file:///…/linux linux-稀疏-测试(这里暗示)然后丢失的文件都作为删除文件添加到索引。因此,如果有人知道等价于git clone——filter=blob:none用于git读取,我们可能可以节省更多兆字节。(阅读git-rev-list的手册页还提示有像——filter=sparse:path=…这样的东西,但我也没有让它工作。

(都是用Debian Buster的git 2.20.1尝试过的。)

其他回答

我有一个类似的用例,除了我只想签出一个标记的提交和修剪目录。使用——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>

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

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


试试这个:

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

现在你会发现你有一个“修剪”签出,只有从路径/within_repo/到/desired_subdir的文件存在(并且在该路径中)。

注意,在windows命令行中,你不能引用路径,也就是说,你必须用这个命令来改变第6个命令:

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

如果您不这样做,您将在稀疏签出文件中获得引号,并且它将不起作用

痛苦的奥罗拉

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

指定要克隆的文件夹

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

Git克隆有一个选项(——no-checkout或-n),可以做你想做的事情。

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

git clone <path>

:

git clone --no-checkout <path>

然后,您可以使用问题中所述的稀疏签出。

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

这样做不错

D:\Lab>git svn clone https://github.com/Qamar4P/LolAdapter.git/trunk/lol-adapter -r HEAD

-r HEAD将只下载最近的版本,忽略所有历史。 注意trunk和/specific文件夹

复制并更改URL在/trunk/之前和之后。我希望这能帮助到一些人。享受:)

2019年9月26日更新