我的使用场景:
1、想用一颗repository树来保存相互之间没有关联、没有依赖的运维子项目,而每个子项目代码量都很少,每一个子项创建一个repository太没有必要了;
2、公司的所有内部api也想统一放置到一颗repository树上,几十个api不能都创建一个repository吧(我现在是这么认为的,这个需求也可能不太合理)。
如果非要只clone repository中的几个子目录的话,那就用sparse clone,git从1.7.0开始支持,sparse clone也只是一个变通的方法:先拿到整个repository的object等元数据信息,然后在本地加一个叫.git/info/sparse-checkout的文件(即黑名单、白名单,支持正则,参见下文具体操作命令)来控制pull那些目录和文件(类似.gitignore文件,都是本地的概念),变通的实现《git只clone仓库中指定子目录和文件》,如果非要完美的满足这个需求那就用svn吧。
引用stackoverflow上对sparse clone的描述:
Implementing something like this in Git would be a substantial effort and it would mean that the integrity of the clientside repository could no longer be guaranteed. If you are interested, search for discussions on “sparse clone” and “sparse fetch” on the git mailinglist.
In general, the consensus in the Git community is that if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories. You can glue them back together using Git Submodules.
具体做法:
一、svn的实现:svn因为是基于文件的集中控制方式,所有“原生”就支持只checkout指定子目录,并且还能很好的对子目录进行权限控制
➜ svn-test svn co http://xxx.xxxx.com/ops/内网服务器情况 test
A test/内网机器硬件配置详细
A test/内网机器硬件配置详细/192.168.1.147.txt
A test/最新全公司网络拓扑图.png
1 2 3 4 5 6 7 8 9 10 11 |
Checked out revision 251. ➜ svn-test ➜ svn-test svn info Path: . Working Copy Root Path: /Users/laijingli/svn-test URL: http://xxx.xxxx.com/ops/%E8%BF%90%E7%BB%B4%E6%96%87%E6%A1%A3 Repository Root: http://xxx.xxxx.com/ops Repository UUID: 5773cb3d-14e2-48da-bdf0-37bd7e579499 Revision: 251 Node Kind: directory Schedule: normal |
二、git的实现:基于sparse clone变通方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[root@vm_test backup]# mkdir devops [root@vm_test backup]# cd devops/ [root@vm_test devops]# git init #初始化空库 Initialized empty Git repository in /backup/devops/.git/ [root@vm_test devops]# git remote add -f origin http://laijingli@192.168.1.1:90/scm/beeper/yunxxx_ops.git #拉取remote的all objects信息 Updating origin remote: Counting objects: 70, done. remote: Compressing objects: 100% (66/66), done. remote: Total 70 (delta 15), reused 0 (delta 0) Unpacking objects: 100% (70/70), done. From http://192.168.1.1:90/scm/beeper/yunxxx_ops * [new branch] master -> origin/master [root@vm_test devops]# git config core.sparsecheckout true #开启sparse clone [root@vm_test devops]# echo "devops" >> .git/info/sparse-checkout #设置需要pull的目录,*表示所有,!表示匹配相反的 [root@vm_test devops]# more .git/info/sparse-checkout devops [root@vm_test devops]# git pull origin master #更新 From http://192.168.1.1:90/scm/beeper/yunxxx_ops * branch master -> FETCH_HEAD [root@vm_test devops]# ls devops [root@vm_test devops]# cd devops/ [root@vm_test devops]# ls monitor_in_web test.1 |
截图:
文章来源:http://blog.csdn.net/xuyaqun/article/details/49275477