如何将 1 个 git 存储库链接到其他一些存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4377573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:36:39  来源:igfitidea点击:

How to Link 1 git repository to some other repositories?

gitrepository

提问by Behrouz.M

How to Link 1 git repository to some other repositories ?

如何将 1 个 git 存储库链接到其他一些存储库?

Assume I have following repositories :

假设我有以下存储库:

/var/Common.git

/var/Common.git

/var/Project1.git

/var/Project1.git

/var/Project2.git

/var/Project2.git

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

现在,我想在其他存储库中使用Common.git。我该怎么做 ?

回答by Cascabel

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:

嵌入的一个关键词是:Common.git 的实际克隆将嵌入到每个其他项目中。当您不打算在其他项目中修改它时,这通常很有用,只需使用一个版本,然后不时从原始 Common.git 更新该版本。你会做这样的事情:

# 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.

请注意,子模块的路径将提交到您的存储库,因此您应该使用公开可用的 URL。如果想在本地自定义,可以运行git submodule init,编辑.git/config中的url,然后运行git submodule update。如果您还有其他问题,请查阅联机帮助页或搜索 SO;这里有很多子模块问题。

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.

另一方面,如果您要在每个项目中编辑 Common.git 的内容,您可能需要使用git-subtree,它是 git 子树合并功能的友好包装器。这将使您将 common.git 的内容视为每个项目内的跟踪内容,同时仍然能够将提交拆分并合并到 Common.git 本身,并将 Common.git 的更新合并回项目中.

回答by Lakshman Prasad

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

这是一个完美的git submodule设计案例:http: //git-scm.com/docs/git-submodule

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

在 Project1 和 Project2 中,添加 Common 的子模块。然后你git submodule checkout

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

在克隆的 repo 中,它只存储 Common git 的哈希值。所以你git submodule init和结帐。