起因:学习os的时候想要看看linux的内核代码如何写的,就想在github上把源码clone下来,因为研究内核所以只想要clone `\kernel`下的文件,搜寻了一下之后,在stackoverflow上找到了解决方式,原文链接git - Is it possible to do a sparse checkout without checking out the whole repository first? - Stack Overflow
为什么要选择只clone一个文件夹?因为linux系统项目很大,直接git clone耗费的时间太长了,所以clone所需文件夹更快
下面是命令,适用于git 2.34.1 ubuntu
$ git clone --filter=blob:none --no-checkout --depth 1 --sparse <project-url>
$ cd <project>
$ git sparse-checkout add <folder1> <folder2>
$ git checkout
win下我没有尝试,有需要的同学可以试一下
ps: 如果你也是想在win下clone linux的源码的话好像不太行,因为Linux的一些系统文件在win下不支持,会直接报错