使用submodule出现的问题

使用需求

存在一套公共的使用库,被很多其它项目使用,又或者很多APP使用的H5页面需要挂到PC项目静态文件中,单纯的代码放到PC项目下,维护又需要拉取整个PC项目修改后进行提交,很不友好,这时候submodule就可以很友好的解决这些问题!

开始

使用

git clone https://github.com/masters.git   //拉取主线
git submodule add https://github.com/public.git  public// 在主线库中添加submodule  

通过以上两步,主线根目录下会出现.gitmodules,是配置文件(有多个子模块,则此文件中将有多个条目),用于存储项目的URL和您将其放入其中的本地子目录之间的映射:

[submodule "public"]
    path = public
    url = https://github.com/public.git

主线目录下查看.git文件(如果隐藏,勾选查看隐藏文件),文件中有一个config文件也会有对应的配置

  • (以上)两个地方会出现对应配置

初始化

克隆一个包含子仓库的仓库目录,并不会clone下子仓库的文件,只是会克隆下.gitmodule描述文件,需要进一步克隆子仓库文件。

cd masters
git submodule init
git submodule update

或者用一行命令
cd masters
git submodule update --init --recursive

此时子目录在一个未命名分支,此时子仓库有改动并没有检测到。

拉取submodule子模块内容

在子仓库,切换到master分支,并git pull最新代码之后,回到主仓库目录,会显示子仓库修改,需要在主仓库提交修改,即修改指定的commit id。

  // public 相当于子模块目录(子模块仓库)直接在当前文件夹下
 git pull

更新

如果在本地修改子仓库,在主仓库 git status会显示子仓库有修改。

 git status
//信息
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

        modified:   public (new commits, untracked content)

no changes added to commit (use "git add" and/or "git commit -a")

需要现在子仓库提交,然后再到主仓库提交代码。

删除子模块

  • 删除.git/config 文件里的关联 (主线)
  • 删除.gitmodules里的关联
  • rm -rf .git/modules/public (也可手动去删除) .git文件夹下面modules还存在子模块

\color{red}{注意 错误 }
1.手动删除子模块,并提交主线,再一次add子模块出现错误

A git directory for 'submoduleSon' is found locally with remote(s):
  origin        https://github.com/public.git
If you want to reuse this local git directory instead of cloning again from
  https://github.com/public.git
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.

出现这样的报错.git/config文件和.gitsubmodule文件存在当前public关联(未删除);
手动再去删除两个文件中的关联也不行;

用 git Base here 窗口
cd .git/modules // 切到.git文件下的modules
ls // 查看目录

目录: C:\工作文件夹\测试\submodule\submoduleFather\.git\modules
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020/12/15      9:39                submodulePublic
d-----       2020/12/15     14:40               pulic

//发现public还存在(执行删除)
rm -rf public
ls
目录: C:\工作文件夹\测试\submodule\submoduleFather\.git\modules
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020/12/15      9:39                submodulePublic

//三个地方删除完毕
git submodule add https://github.com/public.git (OKle)

如果关联的子模块名称和目录文件一致 还需要删除掉中心生成如node_modules

// 常见五步走 完美去掉关联关系
rm -rf 子模块目录 删除子模块目录及源码
.gitmodules 删除项目目录下.gitmodules文件中子模块相关条目
.git/config    删除配置项中子模块相关条目
rm .git/module   删除模块下的子模块目录,每个子模块对应一个目录,注意只删除对应的子模块目录即可
git rm —cached 子模块名称
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容