适用于Linux和Windows的Git子模块

时间:2014-01-27 12:45:59

标签: git git-submodules

在Linux上使用git submodule时,我可能会有一个.gitmodules,其中包含一个或多个子系统,例如/mnt/gitrepos/subsystem1.git(基于中央文件的访问权限)。

[submodule "subsystem1"]
  path = subsystem11
  url = /mnt/gitrepos/subsystem1.git/

我还想支持可以从H:/gitrepos/subsystem1.git/访问相同模块仓库的Windows用户。

例如来自Linux /mnt/gitrepos/的Samba共享作为共享gitrepos

Git是否可以根据操作系统处理URL行? 对于Windows,.gitmodules将是

[submodule "subsystem1"]
  path = subsystem11
  url = H:/gitrepos/subsystem1.git/

因此,我希望.gitmodules(推测语法)的这个通用代码“类似”:

[submodule "subsystem1"]
  path = subsystem11
  if Linux 
    url = /mnt/gitrepos/subsystem1.git/
  else
    url = H:/gitrepos/subsystem1.git/

2 个答案:

答案 0 :(得分:8)

没有。子模块通常设计用于一个可在任何地方工作的存储库URL,通常基于网络(例如git://host/path)。没有为一个存储库提供多个不同URL的机制。

也就是说,Git允许您自定义子模块的URL。初始化子模块(git submodule init)时,.gitmodules中的URL将复制到您的.git/config文件中。您现在可以在运行git submodule update之前修改其中的网址。

答案 1 :(得分:0)

我考虑过(但未尝试过)的一种解决方法是将子模块代码库(在您的示例中为“subsystem1.git”)初始化为独立的本地存储库。在子模块中添加一个“远程”,指向存储库的独立本地版本。还要在子系统存储库的非子模块(独立,本地)版本中添加“远程”,该版本指向子模块版本。然后,您应该能够使用Samba / Windows / Linux更新subsystem1.git的独立(非子模块)版本,然后从子模块中“git fetch independent”将代码从非子模块存储库复制到子模块库。

最后,您的目录结构应如下所示:

/mnt/gitrepos/
/mnt/gitrepos/subsystem1.git/
/mnt/subsystem1.git/

每个存储库中的遥控器如下所示:

/mnt/gitrepos/.git/config would have one remote block:
    [remote "origin"]   (points to your main, non-local repository)
/mnt/subsystem1.git/.git/config would have two remote blocks:
    [remote "origin"]   (points to your main, non-local repository)
    [remote "local_origin"]   (points to /mnt/gitrepos/subsystem1.git)
/mnt/gitrepos/.git/modules/subsystem1.git/config would also have two remote blocks:
    [remote "origin"]   (points to your main, non-local repository)
    [remote "independent"]   (points to /mnt/subsystem1.git)

将子模块的代码与远程服务器上的代码同步然后变为多步骤过程,因为您必须使用“独立”存储库作为中间存储库/临时区域。

你的里程可能会变化......这只是一件看起来应该有效的事情,而不是我尝试过的事情。