将多个 git 存储库克隆到同一目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13549045/
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
multiple git repositories cloned into the same directory
提问by Jamie
I've got a standard repository for my project
我的项目有一个标准的存储库
/home/repo/.git
this is the repository i clone to get the base code for new websites i.e. i cloned this to
这是我克隆的存储库以获取新网站的基本代码,即我将其克隆到
/var/www/site1
i also have several modules that i've created as repositories, some websites will be using these modules and some will not.
我还创建了几个模块作为存储库,有些网站会使用这些模块,有些则不会。
/home/modules/mod1/.git
/home/modules/mod2/.git
is there a way that i can clone those modules into the same site folder?
有没有办法可以将这些模块克隆到同一个站点文件夹中?
/var/www/site1
the module directories are set up with the same folder structure as the master repo, when i clone them on top of the master repo clone they should merge/replace existing files. (rarely any file overlap)
模块目录设置为与主存储库具有相同的文件夹结构,当我将它们克隆到主存储库克隆之上时,它们应该合并/替换现有文件。(很少有任何文件重叠)
my optimal solution would be naming the repo's in some way so that when i deploy a new site I do something like:
我的最佳解决方案是以某种方式命名存储库,以便在部署新站点时执行以下操作:
cd /var/www/newsite
git clone /home/repo/.git
git clone /home/modules/mod1/.git
git clone /home/moudles/mod2/.git
and when I have updates to make to the site I could do a pull like:
当我要对网站进行更新时,我可以执行以下操作:
git pull origin master
git pull mod1
git pull mod2
or preferably:
或最好:
git pull origin master
would also call the pulls on mod1
and mod2
.
也会调用 pullmod1
和mod2
。
I've been looking at git submodules and branches but can't figure out if they are what I need.
我一直在查看 git 子模块和分支,但不知道它们是否是我需要的。
采纳答案by gcbenison
Here's a suggested slight change to your desired deploy commands:
以下是对您所需的部署命令的建议轻微更改:
cd /var/www/
git clone /home/repo/.git newsite
git remote add mod1 /home/modules/mod1/.git
git remote add mod2 /home/moudles/mod2/.git
cd newsite
git merge mod1/master
git merge mod2/master
Explanation:
解释:
As you said, your main module (/home/repo) and your mod1
, mod2
etc. have the same folder structure. Submodules then aren't really appropriate since a submodule has to live in a subdirectory of your main repo. In this version, your local git repository (created by the clone
) knows about three remote repositories. After the merge
operations, its state won't be the same as any of them. The merge
of mod1 will bring in any new files "owned" by mod1. You may get merge conflicts if any files in home/repo
have the same name as those in mod1
, etc. You could supply a flag to merge
to select the version from mod1
always, which I believe you said is the behavior you want.
正如你所说,你的主要模块(/家/回购)和你的mod1
,mod2
等有相同的文件夹结构。子模块则不太合适,因为子模块必须位于主存储库的子目录中。在此版本中,您的本地 git 存储库(由 创建clone
)知道三个远程存储库。在之后merge
的操作,其状态也不会与任何人。该merge
MOD1将在“拥有”,由MOD1任何新文件带来。如果 中的任何文件与home/repo
中的文件同名,您可能会遇到合并冲突mod1
。您可以提供一个标志以merge
从mod1
始终选择版本,我相信您说的是您想要的行为。
回答by gliptak
回答by mvp
In short, you cannot have one git repo to be as sudirectory of another.
简而言之,您不能将一个 git 存储库作为另一个的子目录。
There is one exception to this rule: you can have one git repo as submoduleof another, but this has a lot of limitations and inconveniences.
这条规则有一个例外:你可以将一个 git repo 作为另一个的子模块,但这有很多限制和不便。
General approach to this is to have one master git repo which has no actual convent of its own, but only tracks few git repos as its submodules.
对此的一般方法是拥有一个主 git repo,它没有自己的实际修道院,但只跟踪少数 git repos 作为其子模块。
Alternatively, I would recommend using repo
tool developed by Android project exactly for this purpose. It involves creating small git repository containing just one XML file (called manifest) which tracks where your sub-projects are checked out into and how they are glued together. This works really well on Linux and Mac, but unfortunately does not support Windows (repo requires symbolic link support by OS).
或者,我建议repo
完全为此目的使用由 Android 项目开发的工具。它涉及创建仅包含一个 XML 文件(称为清单)的小型 git 存储库,该文件跟踪您的子项目检出的位置以及它们如何粘合在一起。这在 Linux 和 Mac 上非常有效,但不幸的是不支持 Windows(repo 需要操作系统支持符号链接)。
In certain sense, git submodules and android repo solve essentially same task, but using slightly different means.
从某种意义上说,git submodules 和 android repo 解决了本质上相同的任务,但使用的方法略有不同。