Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Link 1 git repository to some other repositories?

Tags:

git

repository

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 ?

like image 554
Behrouz.M Avatar asked Dec 07 '10 14:12

Behrouz.M


People also ask

How do I link a Git repo to another repo?

git submodule Once the project is added to your repo, you have to init and update it. will fetch the latest changes from upstream in each submodule, merge them in , and check out the latest revision of the submodule.

Can a Git repository contain another Git repository?

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.


2 Answers

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.

like image 102
Cascabel Avatar answered Sep 24 '22 07:09

Cascabel


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.

like image 42
lprsd Avatar answered Sep 24 '22 07:09

lprsd