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

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

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


当前回答

我是新的git,但似乎如果我做每个目录的git签出,然后它工作。同样,稀疏签出文件需要在每个目录后面都有一个斜杠。有人更有经验,请确认这将工作。

有趣的是,如果签出不在稀疏签出文件中的目录,似乎没有什么区别。它们不会显示在git状态中,git read-tree -m -u HEAD不会导致它被删除。Git reset -hard也不会导致目录被删除。有没有更有经验的人愿意评论一下git对签出但不在稀疏签出文件中的目录的看法?

其他回答

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

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]

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

这样做不错

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日更新

在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命令仍然会克隆出完整的历史记录,尽管没有检出文件。如果你不需要完整的历史,你可以在命令中添加——depth参数,如下所示:

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

在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

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

在我的情况下,我想跳过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