如何删除Git子模块?
顺便说一句,有什么理由我不能简单地做
git submodule rm whatever
?
如何删除Git子模块?
顺便说一句,有什么理由我不能简单地做
git submodule rm whatever
?
一旦你表达了对子模块的兴趣,就没有瓷器方式说“我不再对这个子模块感兴趣”
submodule init
”。
“submodule deinit
“是这样做的方式。
删除过程也使用 git rm
(自2013年10月git1.8.5)。
然后,三步删除过程将是:
0. mv a/submodule a/submodule_tmp
1. git submodule deinit -f -- a/submodule
2. rm -rf .git/modules/a/submodule
3. git rm -f a/submodule
# Note: a/submodule (no trailing slash)
# or, if you want to leave it in your working tree and have done step 0
3. git rm --cached a/submodule
3bis mv a/submodule_tmp a/submodule
rm -rf
:这是在提到的 丹尼尔施罗德的 回答,并总结 Eonil 在 评论:
这离开了
.git/modules/<path-to-submodule>/
不变。
因此,如果您曾使用此方法删除子模块并再次重新添加它们,则无法实现,因为存储库已损坏。
git rm
:见 提交95c16418:
目前使用“
git rm
“在子模块上,从超级项目和索引的gitlink中删除子模块的工作树。
但子模块的部分在.gitmodules
保持不变,这是现在删除的子模块的剩余部分,可能会激怒用户(而不是设置为.git/config
,这必须保留用户对此子模块表现出兴趣的提示,以便稍后在签出旧提交时重新填充)。让“
git rm
“通过不仅从工作树中删除子模块,还通过删除”帮助用户“submodule.<submodule name>
“部分来自.gitmodules
档案和舞台两者。
git submodule deinit
:它起源于 这个补丁:
随着“
git submodule init
“用户能够告诉git他们关心一个或多个子模块,并希望在下次调用时填充它”git submodule update
”。
但目前没有简单的方法他们可以告诉git他们不再关心子模块并想要摆脱本地工作树(除非用户对子模块内部有很多了解并删除了“submodule.$name.url
“来自.git/config
与工作树本人一起)。通过提供'帮助这些用户'
deinit
'命令。
这个 删除整个submodule.<name>
部分来自.git/config
要么是给定的 子模块(S) (或者对于所有已经初始化的人,如果'.
' 给出)。
如果当前工作树包含修改除非被强制,则失败。
抱怨在命令行上给出的子模块时,无法找到url设置.git/config
但是,尽管如此,不要失败。
如果(de)初始化步骤(.git/config
和 .git/modules/xxx
)
从git1.8.5开始 git rm
需要 也 照顾:
add
'步骤,记录子模块中的子模块 .gitmodules
文件:需要为您删除。git rm --cached path_to_submodule
(没有尾随斜线)如果您忘记了最后一步,并尝试将子模块添加为常规目录,您将收到如下错误消息:
git add mysubmodule/file.txt
Path 'mysubmodule/file.txt' is in submodule 'mysubmodule'
注意:自Git 2.17(2018年第二季度)以来,git子模块deinit不再是shell脚本。
它是对C函数的调用。
看到 提交2e61273, 提交1342476 (2018年1月14日)by Prathamesh Chavan(pratham-pc
)。
(合并 Junio C Hamano - gitster
- 在 提交ead8dbe,2018年2月13日)
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
${GIT_QUIET:+--quiet} \
${prefix:+--prefix "$prefix"} \
${force:+--force} \
${deinit_all:+--all} "$@"
通过页面 Git子模块教程:
要删除子模块,您需要:
.gitmodules
文件。.gitmodules
变化 git add .gitmodules
.git/config
。git rm --cached path_to_submodule
(没有尾随斜线)。rm -rf .git/modules/path_to_submodule
git commit -m "Removed submodule <name>"
rm -rf path_to_submodule
也可以看看: 下面的替代步骤。
只是一个说明。从git 1.8.5.2开始,两个命令将执行:
git rm the_submodule
rm -rf .git/modules/the_submodule
正如@Mark Cheverton的回答正确指出的那样,如果没有使用第二行,即使你现在删除了子模块,剩余的.git / modules / the_submodule文件夹也会阻止将来添加或替换相同的子模块。另外,正如@VonC所提到的, git rm
将完成子模块的大部分工作。
- 更新(07/05/2017) -
只是澄清一下, the_submodule
是项目内子模块的相对路径。例如,它是 subdir/my_submodule
如果子模块在子目录中 subdir
。
正如评论中所指出的那样 其他答案,这两个命令(虽然在功能上足以删除一个子模块),确实留下了痕迹 [submodule "the_submodule"]
部分 .git/config
(截至2017年7月),可以使用第三个命令删除:
git config -f .git/config --remove-section submodule.the_submodule 2> /dev/null
这个问题的大部分答案都是过时的,不完整的或不必要的复杂。
使用git 1.7.8或更新版本克隆的子模块将在您的本地仓库中留下最多四个自身痕迹。删除这四条跟踪的过程由以下三个命令给出:
# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule
# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule
# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule
简单的步骤
git config -f .git/config --remove-section submodule.$submodulename
git config -f .gitmodules --remove-section submodule.$submodulename
git rm --cached $submodulepath
rm -rf $submodulepath
rm -rf .git/modules/$submodulename
请注意: $submodulepath
不包含前导或尾部斜杠。
背景
当你这样做 git submodule add
,它只是添加它 .gitmodules
但是
一旦你这样做了 git submodule init
,它补充说 .git/config
。
因此,如果您希望删除模块,但能够快速恢复它, 然后这样做:
git rm --cached $submodulepath
git config -f .git/config --remove-section submodule.$submodulepath
这是一个好主意 git rebase HEAD
第一和 git commit
最后,如果你把它放在一个脚本中。
另外看看 一个答案 我可以在Git子模块中取消填充吗?。
除了建议,我也不得不这样做 rm -Rf .git/modules/path/to/submodule
能够添加一个具有相同名称的新子模块(在我的情况下,我用原来的替换叉子)
您必须删除该条目 .gitmodules
和 .git/config
,并从历史记录中删除模块的目录:
git rm --cached path/to/submodule
如果你在git的邮件列表上写,可能有人会为你做一个shell脚本。
要删除使用以下内容添加的子模块:
git submodule add blah@blah.com:repos/blah.git lib/blah
跑:
git rm lib/blah
而已。
对于旧版本的git(大约~1.8.5),请使用:
git submodule deinit lib/blah
git rm lib/blah
git config -f .gitmodules --remove-section submodule.lib/blah