我正在使用一个存储库,其中有大量的文件,需要几个小时才能签出。我正在研究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命令将导致以下错误:

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

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


当前回答

我从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步中路径末尾的斜杠“/”。

其他回答

根据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尝试过的。)

在我的例子中,我想在克隆项目时跳过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

备注:如果你想跳过更多文件夹,只需在稀疏签出文件中添加更多行。

不幸的是,上面没有一个对我有用,所以我花了很长时间尝试不同的稀疏签出文件组合。

在我的情况下,我想跳过IntelliJ IDEA配置的文件夹。

以下是我所做的:


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

执行git config core命令。sparsecheckout真实

创建了.git\info\稀疏签出,包含以下内容

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

运行'git checkout——'获取所有文件。


让它正常工作的关键是在文件夹名称后添加/*。

我有1.9

在git 2.27中,看起来git稀疏签出已经进化了。 这个答案中的解决方案并不完全相同(与git 2.25相比)

git克隆<URL>—no-checkout <目录> cd <目录> Git稀疏签出init——cone #只获取根文件 Git稀疏签出设置apps/my_app libs/my_lib # etc,列出要签出的子文件夹 #它们在这个命令后立即被签出,不需要运行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—sparse和git-sparse-checkout add

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

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]