git 是否可以在不先检出整个存储库的情况下进行稀疏检出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4114887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to do a sparse checkout without checking out the whole repository first?
提问by dromodel
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 是否可以与这种存储库很好地配合使用的可能性,因为它支持稀疏检出,但我能找到的每个示例都执行以下操作:
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:
这一系列命令的问题在于原始克隆也进行了检出。如果将 -n 添加到原始 clone 命令,则 read-tree 命令会导致以下错误:
error: Sparse checkout leaves no entry on working directory
错误:稀疏结帐在工作目录上没有留下任何条目
How can do the sparse checkout without checking out all the files first?
如何在不先检出所有文件的情况下进行稀疏检出?
采纳答案by Alexey Grinko
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:
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
# they are checked out immediately after this command, no need to run git pull
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/
请注意,它需要安装 git 2.25 版。在此处阅读更多相关信息:https: //github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/
回答by apenwarr
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
:
请注意,此答案确实从存储库下载了完整的数据副本。该git remote add -f
命令将克隆整个存储库。从手册页git-remote
:
With
-f
option,git fetch <name>
is run immediately after the remote information is set up.
带
-f
选项,git fetch <name>
在远程信息设置后立即运行。
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).
现在你会发现你有一个“修剪”的结帐,只有来自 path/within_repo/to/desired_subdir 的文件(和在那个路径中)。
Note that on windows command line you must not quote the path, i.e. you must change the 6th command with this one:
请注意,在 Windows 命令行上,您不能引用路径,即您必须使用以下命令更改第 6 个命令:
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
如果你不这样做,你会在稀疏结账文件中得到引号,它不会工作
回答by onionjake
Git clone has an option (--no-checkout
or -n
) that does what you want.
Git clone 有一个选项 (--no-checkout
或-n
) 可以执行您想要的操作。
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.
然后,您可以使用问题中所述的稀疏结帐。
回答by sourcedelica
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.
我有一个类似的用例,除了我只想签出标签的提交并修剪目录。使用--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>
回答by J-F Bergeron
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):
我从 pavek 之前发布的 one-liner 中找到了我正在寻找的答案(谢谢!)所以我想在一个适用于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 pathin step 5.
我稍微更改了命令的顺序,但这似乎没有任何影响。关键是第 5 步中路径末尾是否存在尾部斜杠“/” 。
回答by expert
Sadly none of the above worked for me so I spent very long time trying different combination of sparse-checkout
file.
遗憾的是,上述方法都不适合我,所以我花了很长时间尝试不同的sparse-checkout
文件组合。
In my case I wanted to skip folders with IntelliJ IDEA configs.
就我而言,我想跳过带有 IntelliJ IDEA 配置的文件夹。
Here is what I did:
这是我所做的:
Run git clone https://github.com/myaccount/myrepo.git --no-checkout
跑 git clone https://github.com/myaccount/myrepo.git --no-checkout
Run git config core.sparsecheckout true
跑 git config core.sparsecheckout true
Created .git\info\sparse-checkout
with following content
.git\info\sparse-checkout
使用以下内容创建
!.idea/*
!.idea_modules/*
/*
Run 'git checkout --' to get all files.
运行 'git checkout --' 以获取所有文件。
Critical thing to make it work was to add /*
after folder's name.
使其工作的关键是/*
在文件夹名称之后添加。
I have git 1.9
我有 git 1.9
回答by Qamar
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
-r HEAD will only download last revision, ignore all history.
Note trunkand /specific-folder
-r HEAD 只会下载最新版本,忽略所有历史记录。
注意主干和 /specific-folder
Copy and change URL before and after /trunk/
. I hope this will help someone. Enjoy :)
复制和更改之前和之后的 URL /trunk/
。我希望这会帮助某人。享受 :)
Updated on 26 Sep 2019
2019 年 9 月 26 日更新
回答by VonC
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)
git 2.9(2016 年 6 月)将--no-checkout
选项推广到git worktree add
(该命令允许为一个 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)
请参阅Ray Zhang ( ) 的commit ef2a0ac(2016 年 3 月 29 日)。
帮助者:Eric Sunshine ( )和Junio C Hamano ( )。(由Junio C Hamano合并-- --在commit 0d8683c,2016 年 4 月 13 日)OneRaynyDay
sunshineco
gitster
gitster
The git worktree
man pagenow includes:
该git worktree
手册页现在包括:
--[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.
默认情况下,
add
检查出来<branch>
,但--no-checkout
可以用来抑制结账为了使自定义,如配置稀疏结帐。
回答by SANDEEP MACHIRAJU
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]
回答by Axel Beckert
Based on this answerby apenwarrand this commentby MiralI 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:
基于此答案由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 .
So I got down from 2.9GB to 182MB which is already quiet nice.
所以我从 2.9GB 降到了 182MB,这已经很安静了。
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.
我虽然没有让它与git clone --depth 1 --no-checkout --filter=blob:none file:///…/linux linux-sparse-test
(此处提示)一起使用,因为然后丢失的文件都作为已删除的文件添加到索引中。所以如果有人知道git clone --filter=blob:none
for的等价物git fetch
,我们可能可以节省更多的兆字节。(阅读手册页git-rev-list
也暗示有类似的东西--filter=sparse:path=…
,但我也没有让它起作用。
(All tried with git 2.20.1 from Debian Buster.)
(所有尝试都使用来自 Debian Buster 的 git 2.20.1。)