如何链接1个git仓库到其他仓库? [英] How to Link 1 git repository to some other repositories?

查看:650
本文介绍了如何链接1个git仓库到其他仓库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将1个git仓库链接到其他仓库?



假设我有以下仓库:

/var/Common.git



/var/Project1.git



/var/Project2.git



现在,我想使用 Common.git 在其他仓库中。我可以怎么做?

您可能正在寻找子模块
$ b


子模块允许将外部存储库嵌入到专用子目录中源代码树,始终指向特定的提交。


关键字是嵌入的:实际的克隆的Common.git将嵌入其他每个项目中。这通常适用于当您不打算在其他项目中修改它时,只使用一个版本,并立即从原始的Common.git更新该版本。你可以这样做:

 #将Common.git作为一个子模块添加到此回购库中的common b $ b git submodule add /var/Common.git common 
#初始化它,克隆并检出副本
git子模块update --init
#提交子模块
git commit

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



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


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 ?

解决方案

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.

这篇关于如何链接1个git仓库到其他仓库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆