Git sparse-checkout 检出指定目录或文件

根据网上资料整理而来,git 1.7版本后支持的sparse checkout特性,可以指定需要checkout的目录或者文件。

# 设置允许git克隆子目录
git config core.sparsecheckout true

# 创建本地空repo
git init myRepo && cd myRepo

# 设置要克隆的仓库的子目录路径, “*” 是通配符,“!” 是反选
echo deployment >> .git/info/sparse-checkout

# 设置远程仓库地址
git remote add origin ssh://github.com/abc.git

# 用 pull 来拉取代码
git pull origin master

#############################

# 如果需要添加目录,就增加sparse-checkout的配置,再checkout master
echo another_folder >> .git/info/sparse-checkout
git checkout master

 

后来上面方法遇到错误

error: Sparse checkout leaves no entry on working directory

,又找到另一种方法如下。最后发现,如果在shell里执行,sparse-checkout 里的路径需要最后加*,但是如果是git-prompt,则可以不需要最后的/*.

git clone -n https://github.com/tensorflow/models
cd tensorflow
git config core.sparsecheckout true
echo official/resnet/* >> .git/info/sparse-checkout
git checkout master

 

posted @ 2018-10-04 15:52  Pekkle  阅读(18017)  评论(0编辑  收藏  举报