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

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

问题描述

  + main(本地GIT回购)
我有一个GIT回购库,它有子文件夹作为GIT子库。 +子目录1
+ plugin1(创建为本地GIT仓库)
+ plugin2(创建为本地GIT仓库)
+ subdirectory2
+ subdirectory2a
+ plugin3 GIT repo)
+ subdirectory3

plugin1,plugin2,plugin3是子文件夹(subrepos)主要的GIT回购。
插件1,插件2,插件3也作为本地GIT回购启动并具有内容&



我想将plugin1,plugin2,plugin3 GIT subrepos转换为子模块或主GIT回购。

我想单独在插件GIT仓库中进行开发,但仍保留为子文件夹,在主GIT仓库中仍然显示为链接。
我使用GIT Extensions作为开发版本控制GUI。 更改 main 目录,签出master分支,然后执行以下Git命令为plugin1创建一个新的子模块:

  git子模块add(url_to_plugin1_repository)子目录1 / plugin1sm 

这里的url_to_plugin1_repository指向你当前的Git仓库<强> plugin1 即可。将创建一个新目录,称为 subdirectory1 / plugin1sm ,它将跟踪您的远程存储库。我给它一个不同的名称,以便将它与不是子模块的 plugin1 目录区分开来。请注意,Git将从远程URL复制 plugin1sm 目录的数据,而不是从本地复制。话虽如此,如果您在本地 plugin1 存储库中有任何未经改动的更改,则应在执行上述步骤之前提交并推送它们。



此时,从 main 目录执行git状态应显示类似于以下内容的内容:

  $ git status 
#在分支主机上
#要提交的更改:
#(使用git reset HEAD ...来暂停)

#新文件:.gitmodules
#new file:subdirectory1 / plugin1sm

由于您处于 main 目录中,新子模块显示为变更集中的文件。您可以使用以下命令来提交此更改:

  $ git add subdirectory1 / plugin1sm 
$ git commit -m为plugin1创建的子模块
$ git push origin master

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

  $ cd subdirectory1 / plugin1sm 
$ git add& lt; yourfile& gt;
$ git commit -m修改我的文件
$ git push

但是主存储库呢?这里有些有趣的地方。由于您修改了 plugin1sm 子模块,因此它将显示为主存储库变更集中的修改文件。要继续,您可以添加子模块并使用以下命令推送它:

  $ cd ../。 ./ 
$ git add subdirectory1 / plugin1sm
$ git commit -m更新了我的子模块
$ git push origin master< / code>总而言之,子模块中的基本Git工作流程将像往常一样运行,并且在您的



>

strong> main 存储库,则需要记住整个子模块将显示为一个文件。事情变得比我们在这里考虑的简单用例更复杂,但希望这可以让你走上正确的道路。



您可以对 plugin2 plugin3 目录。当你完成创建子模块时,你应该能够删除原来的插件目录。


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

The plugin1, plugin2, plugin3 are subfolders (subrepos) of main GIT repo. Also plugin1, plugin2, plugin3 were initiated as local GIT repos and has content & history added.

I would like to convert plugin1, plugin2, plugin3 GIT subrepos to submodules or main GIT repo.

I would like to do development separately in the plugins GIT repos, but still keep as subfolders, also still show up as a link in main GIT repo. I use GIT Extensions as development version control GUI.

解决方案

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

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.

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

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

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 &lt;yourfile&gt;
$ git commit -m "modified my file"
$ git push

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</code>

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.

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天全站免登陆