如何删除Git子模块?为什么我不能git子模块rm模块名称?
当前回答
git子模块deinit<子模块路径>从.gitmodules中删除节git rm<子模块路径>删除需要从项目中删除的模块文件。将删除的文件添加到git并调用gitadd.gitmodules提交并推送
其他回答
总而言之,这是您应该做的:
设置path_to_submodule变量(无尾随斜杠):
path_to_submodule=path/to/submodule
从.gitmodules文件中删除相关行:
git config -f .gitmodules --remove-section submodule.$path_to_submodule
从.git/config中删除相关部分
git config -f .git/config --remove-section submodule.$path_to_submodule
仅从索引中取消存储并删除$path_to_submodule(以防止丢失信息)
git rm --cached $path_to_submodule
跟踪对.gitmodules所做的更改
git add .gitmodules
提交超级项目
git commit -m "Remove submodule submodule_name"
删除现在未跟踪的子模块文件
rm -rf $path_to_submodule
rm -rf .git/modules/$path_to_submodule
另请参见:备选指导线
您可以使用别名自动化其他人提供的解决方案:
[alias]
rms = "!f(){ git rm --cached \"$1\";rm -r \"$1\";git config -f .gitmodules --remove-section \"submodule.$1\";git config -f .git/config --remove-section \"submodule.$1\";git add .gitmodules; }; f"
将其放入git配置中,然后可以执行:git rms path/to/submodule
所有的答案看起来都过时了。我使用的是git版本2.28.0。一行回答是,
git rm path-to-submodule
然而,即使从源代码管理中删除了子模块,.git/modules/path到子模块仍然包含子模块存储库,.git/config包含其URL,因此您仍然需要手动删除这些子模块:
git config --remove-section submodule.path-to-submodule
rm -rf .git/modules/path-to-submodule
有时,必须使用-f标志:
$ git rm -f img2vec
例如,因为您可能会收到如下错误:
$ git rm img2vec/
error: the following file has changes staged in the index:
img2vec
(use --cached to keep the file, or -f to force removal)
您必须删除.gitmodules和.git/config中的条目,并从历史记录中删除模块的目录:
git rm --cached path/to/submodule
如果你在git的邮件列表上写东西,很可能有人会为你编写一个shell脚本。
如果由于您添加、提交和推送了一个已经是Git存储库(包含.Git)的文件夹而意外添加了子模块,那么您将没有.gitmodules文件可供编辑,或者.Git/config中的任何内容。在这种情况下,您只需要:
git rm --cached subfolder
git add subfolder
git commit -m "Enter message here"
git push
FWIW,在执行git添加之前,我还删除了.git文件夹。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别