從svn轉git也有45個月的時間了,期間也遇到過1些問題,但也深感git的強大,用者自知,這里就不在多言,git目前唯1不能實現的是:不能像svn那樣,針對子目錄設置權限,這與git散布式倉庫的運行機制有關,svn是基于文件方式的集中存儲,Git卻是基于元數據方式散布式存儲文件信息的,它會在每次Clone的時候將所有信息都取回到本地,即相當于在你的機器上生成1個克隆版的版本庫,既然本地有了完全的版本庫,肯定就有所有權限了,所以也就沒辦法針對子目錄的進行權限控制了。
今天說的問題和上邊有點關系,理解了上邊的內容,這個問題也就簡單了:我們想做的是只拉取1個repository中的幾個子目錄的代碼,而非全部庫,從上文的說明中也能看出這是不能實現的,對,在git 1.7.0 之前是不能實現的,git認為如果這樣做的話,倉庫的數據1致性沒法保證,即便你真的這樣做,完全可以把這些不相干聯的子目錄放到不同的repository,repository之間是彼此獨立的,仔細想一想也很有道理。
我的使用處景:
1、想用1顆repository樹來保存相互之間沒有關聯、沒有依賴的運維子項目,而每一個子項目代碼量都很少,每個子項創建1個repository太沒有必要了;
2、公司的所有內部api也想統1放置到1顆repository樹上,幾10個api不能都創建1個repository吧(我現在是這么認為的,這個需求也可能不太公道)。
如果非要只clone repository中的幾個子目錄的話,那就用sparse clone,git從1.7.0開始支持,sparse clone也只是1個變通的方法:先拿到全部repository的object等元數據信息,然后在本地加1個叫.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.
具體做法:
1、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
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⑴4e2⑷8da-bdf0⑶7bd7e579499
Revision: 251
Node Kind: directory
Schedule: normal
2、git的實現:基于sparse clone變通方法
[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
截圖:
很贊的幾篇參考文章(作為子弟,你遇到的很多問題先輩們早已遇到,并且很多已有了完善的解決方案,做技術1定要勤于google?。?/span>
http://stackoverflow.com/questions/600079/is-there-any-way-to-clone-a-git-repositorys-sub-directory-only
http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/
http://schacon.github.io/git/git-read-tree.html#_sparse_checkout
http://www.tuicool.com/articles/QjEvQvr
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
上一篇 ocp-516
下一篇 hdu1430 (bfs)