如何删除Git子模块?
用“
submodule init
” 表示对子模块的兴趣后,瓷器就没有办法说“我不再对此子模块感兴趣”。
“submodule deinit
”是这样做的方法。
删除过程也会使用git rm
(自git1.8.5 2013年10月起)。
三步删除过程如下:
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
: This is mentioned in Daniel Schroeder's answer, and summarized by Eonil in the comments:
This leaves
.git/modules/<path-to-submodule>/
unchanged.
So if you once delete a submodule with this method and re-add them again, it will not be possible because repository already been corrupted.
git rm
: See commit 95c16418:
Currently using "
git rm
" on a submodule removes the submodule's work tree from that of the superproject and the gitlink from the index.
But the submodule's section in.gitmodules
is left untouched, which is a leftover of the now removed submodule and might irritate users (as opposed to the setting in.git/config
, this must stay as a reminder that the user showed interest in this submodule so it will be repopulated later when an older commit is checked out).Let "
git rm
" help the user by not only removing the submodule from the work tree but by also removing the "submodule.<submodule name>
" section from the.gitmodules
file and stage both.
git submodule deinit
: It stems from this patch:
With "
git submodule init
" the user is able to tell git they care about one or more submodules and wants to have it populated on the next call to "git submodule update
".
But currently there is no easy way they can tell git they do not care about a submodule anymore and wants to get rid of the local work tree (unless the user knows a lot about submodule internals and removes the "submodule.$name.url
" setting from.git/config
together with the work tree himself).Help those users by providing a '
deinit
' command.
This removes the wholesubmodule.<name>
section from.git/config
either for the given submodule(s) (or for all those which have been initialized if '.
' is given).
Fail if the current work tree contains modifications unless forced.
Complain when for a submodule given on the command line the url setting can't be found in.git/config
, but nonetheless don't fail.
This takes care if the (de)initialization steps (.git/config
and .git/modules/xxx
)
Since git1.8.5, the git rm
takes also care of the:
add
' step which records the url of a submodule in the .gitmodules
file: it is need to removed for you.git rm --cached path_to_submodule
(no trailing slash)If you forget that last step, and try to add what was a submodule as a regular directory, you would get error message like:
git add mysubmodule/file.txt
Path 'mysubmodule/file.txt' is in submodule 'mysubmodule'
Note: since Git 2.17 (Q2 2018), git submodule deinit is no longer a shell script.
It is a call to a C function.
参见Prathamesh Chavan()的commit 2e61273和commit 1342476(14 Jan 2018 )。(由Junio C Hamano合并--在commit ead8dbe中,2018年2月13日)pratham-pc
gitster
git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
${GIT_QUIET:+--quiet} \
${prefix:+--prefix "$prefix"} \
${force:+--force} \
${deinit_all:+--all} "$@"
您可以举一个使用示例
submodule deinit
吗?@yourfriendzak是成功使用它的一个示例:stackoverflow.com/a/16161950/6309。但是请记住,与我最初的想法相反,1.8.3尚未发布!在Unix上,您可以从源代码进行编译。
@HamishDowner特殊条目应该消失了(目录不再是子模块),并且
.gitmodules
应该可以,但是我仍然会仔细检查.git
目录中的任何内容(即本地配置文件中的本地配置):被git pull
)修改@Jayen是的,如果您
.gitmodules
在索引中提交了删除条目和特殊条目的删除,并推入该存储库,其他人可以将其拉出,并且该子模块将消失。在当前的git(v1.9 +)中,
git rm submodule
就像其他人已经说过的那样,plain old 确实可以满足您的要求。