简体   繁体   English

如何将跟踪文件夹更改为 git 子模块

[英]How to change a tracked folder into a git submodule

I'd like to change one folder in my project into a submodule.我想将项目中的一个文件夹更改为子模块。 This is my (sumplified) directory structure:这是我的(总结)目录结构:

|-app <- the root of git project   
|--themes
|--plugins
|--lib

Now, I want to turn app/plugins into a submodule so that I commit changes in this directory into a separate reposiotry.现在,我想将 app/plugins 变成一个子模块,以便我将此目录中的更改提交到一个单独的存储库中。

After setting up a new repo for plugins, I've tired git submodule add <submodule-repository> plugins , but then I receive a message the reads为插件设置新的 repo 后,我厌倦了git submodule add <submodule-repository> plugins ,但随后我收到一条消息,读取

app/plugins' already exists in the index app/plugins' 已经存在于索引中

Sure it exists in the index since it was tracked as part of the main project repository.肯定它存在于索引中,因为它被作为主项目存储库的一部分进行跟踪。

How should I solve it?我该如何解决? Should I perhaps delete the main .git folder, add app/plugins to .gitignore, commit again, and then run git submodule add ?我是否应该删除主.git文件夹,将app/plugins添加到 .gitignore,再次提交,然后运行git submodule add

This actually raises another question, should a folder that is a submodule of the project be added to .gitignore of the project that includes the submodule?这实际上提出了另一个问题,是否应该将作为项目子模块的文件夹添加到包含子模块的项目的.gitignore中?

I managed to answer my own question.我设法回答了我自己的问题。 Perhaps I posted too soon.可能是我发的太早了。 Nevertheless maybe it will help some people in the future:不过也许它会在未来帮助一些人:

In order to turn a folder tracked in the current project into this project's submodule, I took the following steps:为了将当前项目中跟踪到的文件夹转换为该项目的子模块,我采取了以下步骤:

  1. Initialize a new repository in the folder that will later be a submodule;在文件夹中初始化一个新的存储库,该存储库稍后将成为子模块;
  2. Commit the contents to a remote;将内容提交到远程;
  3. Remove this folder from the main project with git rm -rf <folder_that_will_be_a_submodule> ;使用git rm -rf <folder_that_will_be_a_submodule>从主项目中删除这个文件夹;
  4. Now do the same but with rm -rf ;现在用rm -rf做同样的事情;
  5. Go to your project root (where the main .git lives);转到您的项目根目录(主.git所在的位置);
  6. Execute git submodule add <repository> ;执行git submodule add <repository>
  7. Remove references to the folder that is now a submodule form your main .gitignore (if it has been referenced there).从您的主.gitignore删除对现在是子模块的文件夹的引用(如果它已在那里被引用)。

Hope it will help somebody.希望它会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM