跳转至

Git 子模块添加时报错

本文信息

创建时间: 2022.05.15 17:05:59

更新时间: 2022.10.10 21:16:59

问题

在主库执行如下命令时

Bash
git submodule add https://github.com/lz-wang/PyToolbox.git ./pkg/pytoolbox        
报错如下
Bash
1
2
3
4
5
6
fatal: A git directory for 'pkg/pytoolbox' is found locally with remote(s):
  origin        https://github.com/lz-wang/PyToolbox.git
If you want to reuse this local git directory instead of cloning again from
  https://github.com/lz-wang/PyToolbox.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.
image.png

解决方案

清理本地文件

Bash
rm -rf ./pkg/PyToobox

清理 .gitsubmodule 文件

Bash
rm .gitsubmodule 

清理 .git/config 文件

移除其中的 submodule 字段下的对应库即可

清理 .git/modules 文件

Bash
rm -rf .git/modules/pkg/PyToobox

重新添加 gitsubmodule

Bash
git submodule add https://github.com/lz-wang/PyToolbox.git ./pkg/pytoolbox

参考