How to Link 1 git repository to some other reposit

2019-01-21 17:00发布

How to Link 1 git repository to some other repositories ?

Assume I have following repositories :

/var/Common.git

/var/Project1.git

/var/Project2.git

Now, I want to use Common.git in other repositories. how can I do it ?

2条回答
别忘想泡老子
2楼-- · 2019-01-21 17:38

You're probably looking for submodules:

Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.

A key word there is embedded: an actual clone of Common.git would be embedded inside each of the other projects. This is generally good for when you're not going to modify it inside the other projects, just use one version, and update that version from the original Common.git now and then. You'd do something like this:

# add Common.git as a submodule at the path "common" inside this repo
git submodule add /var/Common.git common
# initialize it, clone, and check out a copy
git submodule update --init
# commit the addition of the submodule
git commit

Note that the path to the submodule is going to be committed to your repository, so you should use a publicly available URL. If you want to customize it locally, you can run git submodule init, edit the url in .git/config, and then run git submodule update. If you have further questions, consult the manpage or search SO; there are plenty of submodule questions here.

If on the other hand you're going to be editing the contents of Common.git inside each of the projects, you may want to use git-subtree, which is a friendly wrapper around git's subtree merge faculties. That will let you regard the contents of common.git as tracked content inside each of the projects, while still being able to split out commits to it and merge them into Common.git itself, and merge updates to Common.git back into the projects.

查看更多
该账号已被封号
3楼-- · 2019-01-21 17:52

This is a perfect case for which git submodule was designed: http://git-scm.com/docs/git-submodule

Within the Project1 and Project2, you add submodule of the Common. And then you git submodule checkout

In the cloned repo it only stores the hash of the Common git. So you git submodule init and checkout.

查看更多
登录 后发表回答