git仓库包含子仓库时,add报错的解决办法

本文主要介绍在一个仓库内部克隆另外一个仓库,后续该如何进行管理。最近在尝试使用 Hexo 博客,在添加主题后使用 git 的 add 命令时报错。

在父仓库内部克隆一个仓库 /themes/hexo-theme-huhu (从另一个 rope 克隆过来的)

在使用 git add . 命令时遇到问题,报错信息如下:

warning: adding embedded git repository: themes/hexo-theme-huhu
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> themes/hexo-theme-huhu
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached themes/hexo-theme-huhu
hint: 
hint: See "git help submodule" for more information.

push 完之后,在 GitHub 上会显示如下图的图标,代表这是一个子模块,但是不知道这个模块仓库所在的 url ,因此在 GitHub 上无法打开这个文件夹。

git仓库包含子仓库时,add报错的解决办法 经验总结 第1张

解决方案

不使用 git submodule 功能,而是直接将这个文件夹作为根仓库的内容加入并 commit 。

1、删除已经 staged 过的文件:

git rm --cached themes/hexo-theme-huhu

2、查看当前状态:

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    themes/hexo-theme-huhu

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        themes/hexo-theme-huhu/

3、重新 stage 这个文件夹

git add themes/hexo-theme-huhu/

注意:这里最后一定要加上  / ,表示将这个文件夹加入,而不是将这个文件夹当做一个子模块。

两者区别:

git add themes/hexo-theme-huhu/ : create mode 100644

git add themes/hexo-theme-huhu : create mode 160000

4、再次执行 add 、 commit,并 push ,这样就可以正常 push 上去了。

git仓库包含子仓库时,add报错的解决办法 经验总结 第2张

引申

因为我没有 hexo-theme-huhu 这个仓库的 push 权限,所以直接使用包含文件夹的形式更为方便。

其实 git 的 submodule 功能特别强大,能够将父仓库与子仓库分开管理。

关于 submodule 的使用推荐详细地读一下 Pro Git:中文版英文版


推荐阅读:

Ueditor后端配置项没有正常加载,上传插件不能正常使用!的解决过程。

解决margin击穿的几种方法

Z-Blog 搜索报错:Cannot pass parameter 1 by reference 解决方法

input[type=file]去掉“未选择任何文件”及样式改进

设置 letter-spacing 后文字不能居中的解决方法

赞 (1)
分享到: +

评论 沙发

Avatar

换个身份

  • 昵称 (必填)
  • 邮箱 (选填)