git如何设置嵌套仓库(设置子树或子模块),并解决直接将一个仓库拖拽到另一个仓库中导致的问题

git 将一个仓库拷贝到另一个仓库的文件夹下。默认git并不会处理,上传上去之后,只会创建一个文件夹,但是这个文件夹点不开。

git add . 的时候,会报出警告:

bash 复制代码
警告:正在添加嵌入式 git 仓库:client
提示: You've added another git repository inside your current repository.
提示: Clones of the outer repository will not contain the contents of
提示: the embedded repository and will not know how to obtain it.
提示: If you meant to add a submodule, use:
提示:
提示:  git submodule add <url> client
提示:
提示: If you added this path by mistake, you can remove it from the
提示: index with:
提示:
提示:  git rm --cached client
提示:
提示: See "git help submodule" for more information.
提示: Disable this message with "git config advice.addEmbeddedRepo false"

这个意思是,正在将一个嵌套的 Git 仓库(子仓库)添加到当前的 Git 仓库中。具体来说,client 目录内似乎已经是一个独立的 Git 仓库,这会导致一些问题:

嵌套 Git 仓库:

Git 检测到 client 目录中有一个 .git 子目录,这意味着 client 目录本身是一个 Git 仓库(即子仓库或嵌套仓库)。

git并不知道,你想要怎么处理该仓库。所以直接将这样的子仓库添加到父仓库中,会导致问题------父仓库不会自动处理子仓库的内容和版本控制。

因此需要显式的指定添加的是什么:

如果是添加一个子模块,则采用 git submodule add <url> client

如果是添加一个子库,并不应该直接将该库拖拽进来。

具体解决方案:

1. 如果 client 应该是子模块 :

如果 client 目录实际上应该是一个子模块,那么你需要正确地将其添加为子模块:

bash 复制代码
git rm --cached client
git submodule add <url> client
  • <url> 是子模块的 Git 仓库 URL。
  • git rm --cached client 从当前仓库的索引中移除 client 目录(不会删除实际目录)。

2. 如果 client 应该是子树 :

如果 client 目录实际上应该作为子树的一部分,你需要从父仓库中移除它,然后再正确地将其作为子树添加。

bash 复制代码
git rm --cached client
rm -rf client
git commit -m "Remove client submodule"

然后重新将 client 作为子树添加到父仓库:

bash 复制代码
git subtree add --prefix=client <url> branch-name
  • <url> 是子树的 Git 仓库 URL。
  • branch-name 是子树仓库中的分支名称。

添加之后,会直接生成一个commit

总结

  • 子模块 : 如果 client 目录应该是一个独立的仓库并且被作为子模块管理,使用 git submodule add
  • 子树 : 如果 client 目录应该嵌入到父仓库中作为子树,先移除它,再用 git subtree add 重新添加。

通过这些步骤,就可以解决嵌套仓库导致的问题,并确保 client 目录被正确地添加到父仓库中。

相关推荐
汤永红30 分钟前
windows下git bash安装SDKMan报错Looking for unzip...Not found
java·git·sdkman
NoCoding@程序猿41 分钟前
Mac idea Word&Excel等文件git modify 一直提示修改状态
git
Yang-Never1 小时前
Git -> Git使用Patch失败error: patch failed: patch does not apply的解决办法
android·git·android studio
TDengine (老段)3 小时前
TDengine 语言连接器(Node.js)
大数据·c语言·数据库·物联网·node.js·时序数据库·tdengine
Bryan_Long4 小时前
Git 命令行操作提交至远程仓库
git·github
极小狐5 小时前
极狐GitLab 功能标志详解
linux·运维·服务器·elasticsearch·gitlab·极狐gitlab
jinan8865 小时前
加密软件的发展:从古典密码到量子安全
大数据·运维·服务器·网络·安全·web安全
微臣愚钝6 小时前
git的上传流程
git
lmxnsI6 小时前
git报错
git
CC数学建模6 小时前
第十七届“华中杯”大学生数学建模挑战赛题目A题 晶硅片产销策略优化 完整成品 代码 模型 思路 分享
大数据