如何删除Git子模块?为什么我不能git子模块rm模块名称?
当前回答
自git1.8.3(2013年4月22日)起:
一旦你用“gitsubmoduleInit”表达了对某个子模块的兴趣,就没有瓷的方式来表示“我不再对这个子模块感兴趣了”。“git submodule deinit”是这样做的方法。
删除过程还使用gitrm(自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/<子模块路径>/保持不变。所以,若您使用此方法删除了一个子模块并再次添加它们,那个么就不可能了,因为存储库已经损坏。
git rm:请参阅提交95c16418:
当前在子模块上使用“gitrm”会将子模块的工作树从超级项目的工作树中删除,并将gitlink从索引中删除。但是.gitmodules中的子模块部分保持不变,这是现在删除的子模块的剩余部分,可能会激怒用户(与.git/config中的设置相反,这必须保持提醒用户对该子模块感兴趣,以便稍后在签出旧的提交时重新填充)。
让“gitrm”不仅从工作树中删除子模块,还可以从.gitmodules文件中删除“submodule.<submodulename>”部分,并将两者都暂存,从而帮助用户。
git子模块deinit:它源于这个补丁:
使用“gitsubmodule init”,用户可以告诉git他们关心一个或多个子模块,并希望在下次调用“gitsubmodule update”时填充它。但目前,他们无法简单地告诉git他们不再关心子模块,并希望摆脱本地工作树(除非用户非常了解子模块的内部结构,并将“submodule.$name.url”设置与工作树一起从.git/config中删除)。
通过提供“deinit”命令帮助这些用户。这将删除整个子模块<对于给定的子模块(或所有已初始化的子模块,如果给定“.”)。如果当前工作树包含修改,则失败,除非强制。如果在命令行上给定的子模块的url设置在.git/config中找不到,但仍然不会失败,请进行投诉。
如果(de)初始化步骤(.git/config和.git/modules/xxx)
从git1.8.5开始,gitrm还负责:
在.gitmodules文件中记录子模块url的“add”步骤:需要为您删除它。子模块特殊条目(如这个问题所示):gitrm将其从索引中删除:git rm—缓存的path_to_submodule(无尾斜杠)这将以特殊模式“160000”删除索引中存储的目录,并将其标记为子模块根目录。
如果您忘记了最后一步,并尝试将子模块添加为常规目录,则会收到如下错误消息:
git add mysubmodule/file.txt
Path 'mysubmodule/file.txt' is in submodule 'mysubmodule'
注意:自从Git 2.17(2018年第二季度)以来,Git子模块deinit不再是shell脚本。这是对C函数的调用。
见Prathamesh Chavan(pratham pc)的承诺2e61273、承诺1342476(2018年1月14日)。(于2018年2月13日由Junio C Hamano--gitster--在commit ead8dbe中合并)
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
${GIT_QUIET:+--quiet} \
${prefix:+--prefix "$prefix"} \
${force:+--force} \
${deinit_all:+--all} "$@"
其他回答
如果您需要使用bash脚本在一行命令中执行此操作,如下所示:
$cd/path/to/your/rebo&&/bin/bash$HOME/remove_submodule.sh/path/to-the/submodule
在$HOME目录中创建bash脚本文件,名为remove_submodule.sh:
#!/bin/bash
git config -f .gitmodules --remove-section submodule.$1
git config -f .git/config --remove-section submodule.$1
git rm --cached $1
git add .gitmodules
git commit -m "Remove submodule in $1"
rm -rf $1
rm -rf .git/modules/$1
git push origin $(git rev-parse --abbrev-ref HEAD) --force --quiet
这个问题的大多数答案都过时、不完整或不必要的复杂。
使用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
project dir: ~/foo_project/
submodule: ~/foo_project/lib/asubmodule
- - - - - - - - - - - - - - - - - - - - - - - - -
run:
1. cd ~/foo_project
2. git rm lib/asubmodule &&
rm .git/modules/lib/asubmodule &&
git submodule lib/asubmodule deinit --recursive --force
使用git v2.7.4,简单的3个步骤就可以了。
git submodule deinit -f -- a/submodule
git rm -f a/submodule
git commit
我最近发现了一个git项目,其中包含许多有用的git相关命令:https://github.com/visionmedia/git-extras
安装并键入:
git-delete-submodule submodule
然后事情就完成了。子模块目录将从repo中删除,并且仍然存在于文件系统中。然后可以提交更改,如:gitcommit-am“Remove the submodule”。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的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之间的区别