有时在git clone的时候,只想clone自己想要的文件夹或目录。文件可以通过打开raw文件直接下载,而只clone目录似乎没有比较好的办法。Git1.7.0以后加入了Sparse Checkout模式,该模式可以实现Check Out指定文件或者文件夹。
现在有一个test仓库ssh://git@/mygithub/test.git
需要git clone
里面的myproj/models
子目录:
方法一
git init test && cd test// 新建仓库并进入文件夹git config core.sparsecheckout true // 设置允许克隆子目录echo 'myproj/models' >> .git/info/sparse-checkout // 设置要克隆的仓库的相对根目录路径 //空格别漏git remote add origin ssh://git@/mygithub/test.git // 这里换成你要克隆的项目和库git pull origin master // 下载代码
如果只想保留最新的文件而不要历史版本的文件,上例最后一行可以用git pull --dpeth 1
命令,即“浅克隆”:
$ git pull --depth 1 origin master
方法二
step1:From GitURL to SVNURL
点开”/models”子文件夹,复制浏览器中的地址,如下:
https:// github. com/mygithub/test/tree
/master/models
将”/tree/”替换成”/trunk/”,则新生成的新链接为:
https:// github. com/mygithub/trunk
/models
注意:这里根据分支的不同,通用的办法是将”/branches/branchname/”替换成”/trunk/”。
例如:”/tree/master/” to “/trunk/” ; “/tree/develop/” to “trunck”
step2:svn checkout your code
svn checkout SVNURL
这里的SVNURL就是step1中生成的url
方法三
如果觉得以上操作麻烦,可以直接进入这个网站:DownGit