根据网上资料整理而来,git 1.7版本后支持的sparse checkout特性,可以指定需要checkout的目录或者文件。
1# 设置允许git克隆子目录 2git config core.sparsecheckout true 3 4# 创建本地空repo 5git init myRepo && cd myRepo 6 7# 设置要克隆的仓库的子目录路径, “*” 是通配符,“!” 是反选 8echo deployment >> .git/info/sparse-checkout 9 10# 设置远程仓库地址 11git remote add origin ssh://github.com/abc.git 12 13# 用 pull 来拉取代码 14git pull origin master 15 16############################# 17 18# 如果需要添加目录,就增加sparse-checkout的配置,再checkout master 19echo another_folder >> .git/info/sparse-checkout 20git checkout master
后来上面方法遇到错误
error: Sparse checkout leaves no entry on working directory
,又找到另一种方法如下。最后发现,如果在shell里执行,sparse-checkout 里的路径需要最后加*,但是如果是git-prompt,则可以不需要最后的/*.
1git clone -n https://github.com/tensorflow/models 2cd tensorflow 3git config core.sparsecheckout true 4echo official/resnet/* >> .git/info/sparse-checkout 5git checkout master