Git only clones the implementation of specified subdirectories and specified files in the repository 20151020

Source: Internet
Author: User
Tags svn using git

From SVN to git also have four or five months of time, also encountered some problems, but also feel strong git, users know, here is not many words, git the only thing that can not be implemented is: not like SVN, for subdirectories to set permissions, which is related to the operation of Git distributed warehouse, SVN is a file-based centralized storage, GIT is a metadata-based distributed storage of file information, it will be in every clone of the information back to the local, that is equivalent to build a clone version of the repository on your machine, since the local have a full repository, there must be all permissions, Therefore, there is no way to control the permissions of the sub-directory.

Today, the question is a bit related to the above, understand the above content, the problem is simple: we want to do is to pull a repository in a few subdirectories of the code, rather than the entire library, from the above description can also see that this is not possible, yes, Git 1.7.0 before the implementation, Git believes that if you do this, the data consistency of the warehouse is not guaranteed, even if you do, it is possible to put these unrelated subdirectories in different repository,repository is independent of each other, it makes sense to think carefully.


My usage scenario:

1, want to use a repository tree to save each other unrelated, no dependent operations sub-project, and each sub-project code is very small, each child to create a repository too unnecessary;

2, the company's internal API also want to be unified into a repository tree, dozens of API can not all create a repository bar (I now think, this demand may not be reasonable).


If you want to clone only a few subdirectories in the repository, then use sparse clone,git from 1.7.0 to support, sparse clone is only a workaround: First get the entire repository object and other metadata information, Then add a locally called. git/info/sparse-checkout file (i.e. blacklist, whitelist, support regular, see the specific Operation command below) to control pull those directories and files (similar to the. gitignore file, are local concepts), the flexible implementation of the Git only clones the specified subdirectories and files in the repository, and if you want to satisfy this requirement perfectly, then use SVN instead.

Refer to the description of sparse clone on StackOverflow:

Implementing something like this-in Git would is a substantial effort and it would mean that the integrity of the Clientsi De repository could no longer be guaranteed. If you is interested, search for discussions on "sparse clone" and "sparse fetch" on the Git mailinglist.

In general, the consensus in the Git community was if you had several directories that was always checked out Indepen Dently, then these is really, different projects and should live in, different repositories. You can glue them back together using Git submodules.

Specific practices:

First, the implementation of SVN: SVN because it is based on the centralized control of the file, all "native" support only checkout the specified subdirectory, and also good sub-directory permissions control

➜svn-test SVN Co http://xxx.xxxx.com/ops/intranet server condition test
A test/Intranet Machine hardware configuration details
A test/Network Machine hardware configuration details/192.168.1.147.txt
A test/the latest company-wide network topology map. 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-14e2-48da-bdf0-37bd7e579499
revision:251
Node kind:directory
Schedule:normal


Second, GIT implementation: Based on sparse clone workaround

[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 # Pull all objects information for remote
Updating origin
Remote:counting objects:70, done.
Remote:compressing objects:100% (66/66), done.
Remote:total (Delta), 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的目录, * denotes all,! means match the opposite
[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

Screenshots:




Very good several reference articles (as the younger generation, you encounter many problems predecessors have already met, and many have a perfect solution, do the technology must be diligent Google AH):

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.