如何将 Git 存储库转换为嵌套在另一个(父)Git 存储库中的子模块? [英] How to convert a Git repo to a submodule, which is nested in another (parent) Git repo?

查看:26
本文介绍了如何将 Git 存储库转换为嵌套在另一个(父)Git 存储库中的子模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Git 存储库,其中包含子文件夹作为 Git 子存储库.

I have a Git repo which has subfolders as Git subrepositories.

+ main (local GIT repo)
  + subdirectory1
     + plugin1 (created as local GIT repo)
     + plugin2 (created as local GIT repo)
  + subdirectory2
    + subdirectory2a
      + plugin3 (created as local GIT repo)
  + subdirectory3

plugin1、plugin2、plugin3 是主 Git 存储库的子文件夹(子存储库).plugin1、plugin2、plugin3 也作为本地 Git 存储库启动,并添加了内容和历史记录.

The plugin1, plugin2, plugin3 are subfolders (subrepos) of the main Git repo. Also plugin1, plugin2, plugin3 were initiated as local Git repos and have content as well as history added.

我想将 plugin1、plugin2、plugin3 从 Git 子存储库转换为主 Git 存储库中的子模块.

I would like to convert plugin1, plugin2, plugin3 from Git subrepos to submodules inside the main Git repo.

我想在插件的 Git 存储库中单独进行开发,但仍将它们保留为子文件夹.它们还应该在主 Git 存储库中显示为链接.

I would like to do development separately in the plugins' Git repos, but still keep them as subfolders. They should also still show up as links in the main Git repo.

我使用 Git 扩展作为开发版本控制 GUI.

I use Git Extensions as development version control GUI.

推荐答案

切换到ma​​in目录,签出master分支,执行如下Git命令为plugin1新建一个子模块:

Change to the main directory, checkout the master branch, and do the following Git command to create a new submodule for plugin1:

git submodule add (url_to_plugin1_repository) subdirectory1/plugin1sm

此处的url_to_plugin1_repository"指向plugin1 的当前 Git 存储库.将创建一个名为 subdirectory1/plugin1sm 的新目录,它将跟踪您的远程存储库.我给它起了一个不同的名字,以区别于不是子模块的 plugin1 目录.请注意,Git 将从远程 url 克隆 plugin1sm 目录的数据,而不仅仅是从本地复制.话虽如此,如果您在本地 plugin1 存储库中有任何未提交的更改,您应该在执行上述步骤之前提交并推送它们.

Here the "url_to_plugin1_repository" points to your current Git repository for plugin1. A new directory will be created call subdirectory1/plugin1sm, which will track your remote repository. I have given it a different name to distinguish it from the plugin1 directory which is not a submodule. Take note that Git will be cloning the data for the plugin1sm directory from the remote url, rather than just copying from your local. That being said, if you have any uncommited changes in your local plugin1 repository, you should commit and push them before doing the above step.

此时,从 ma​​in 目录执行 git status 应该显示类似于以下内容:

At this point, doing a git status from the main directory should show something similar to the following:

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#   new file:   .gitmodules
#   new file:   subdirectory1/plugin1sm

由于您位于 ma​​in 目录中,新的子模块在变更集中显示为文件".您可以使用以下命令提交此更改:

Since you are in the main directory, the new submodule shows up as a "file" in the changeset. You can commit this change with the following commands:

$ git add subdirectory1/plugin1sm
$ git commit -m "Created submodule for plugin1"
$ git push origin master

您可能会想到的下一个问题是如何将新的子模块与您的主 Git 存储库一起使用.让我们首先看看当您处理 plugin1sm 目录中的文件时会发生什么.当您在 plugin1sm 目录中工作时,Git 将跟踪更改并表现得好像它不知道该目录之外的任何内容.当需要提交和推送更改时,您可以使用以下预期命令:

The next question which will probably come to your mind is how do you go about using the new submodule along with your main Git repository. Let's start by looking into what happens when you work on files inside the plugin1sm directory. When you work inside the plugin1sm directory, Git will track changes and behave as if it doesn't know about anything outside of that directory. When the time comes to commit and push your changes, you use the following expected commands:

$ cd subdirectory1/plugin1sm
$ git add <yourfile>
$ git commit -m "modified my file"
$ git push

但是主存储库呢?这就是事情变得有点有趣的地方.由于您修改了 plugin1sm 子模块,它将在主存储库的变更集中显示为修改后的文件".要继续,您可以添加子模块并使用以下命令推送它:

But what about the main repository? Here is where things get a little interesting. Since you modified your plugin1sm submodule, it will show up as a modified "file" in the changeset of the main repository. To continue, you can add the submodule and push it with the following commands:

$ cd ../../
$ git add subdirectory1/plugin1sm
$ git commit -m "updated my submodule"
$ git push origin master

总而言之,您在子模块中的基本 Git 工作流程将照常进行,并且在您的存储库中,您需要记住整个子模块将显示为一个文件.事情变得比我们在这里考虑的简单用例更复杂,但希望这能让您走上正确的道路.

So to summarize, your basic Git workflow within a submodule will be business as usual, and within your main repository, you will need to keep in mind that the entire submodule will appear as a file. Things get more complex than the simple use case we considered here, but hopefully this sets you on the right path.

您可以对 plugin2plugin3 目录重复此过程.当你完成子模块的创建后,你应该可以删除原来的插件目录.

You can repeat this procedure for the plugin2 and plugin3 directories. And when you have finished creating the submodules, you should be able to delete the original plugin directories.

这篇关于如何将 Git 存储库转换为嵌套在另一个(父)Git 存储库中的子模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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