How to remove a submodule in git ?

How to remove a submodule in git ?

Asked on December 17, 2018 in Git.
Add Comment

  • 3 Answer(s)

    Here the solution:

    The deletion process also uses git rm


    They are  3-steps removal process:

    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 leaves .git/modules/<path-to-submodule>/ unchanged.
    So if you once delete a submodule with this method and re-add them again it is not possible  repository already been corrupted.

    git rm:

    Presently using “git rm”  on a submodule removes the submodule’s work tree

    But the submodule’s section in .gitmodules is left untouched, and now removed submodule and might irritate users as opposed to the setting in .git/config,  that the user showed interest in this submodule .

    git rm help the user by not only removing the submodule from the work tree and removing the “submodule.<submodule name>” section from the .gitmodules file

    git submodule deinit:

    git submodule init the user is able to tell git they care about one or more submodules and  next call to “git submodule update”.

    the user knows a lot about submodule internals and removes the “submodule.$name.url” setting from .git/config together with the work tree

    his removes the whole submodule.<name> section from .git/config either for the given submodule(s

    git rm --cached path_to_submodule
    final step, submodule as a regular directory,
    git add mysubmodule/file.txt
    Path 'mysubmodule/file.txt' is in submodule 'mysubmodule'


    git submodule deinit is no longer a shell script.
    It is a call to a C function.

    git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit \
    ${GIT_QUIET:+--quiet} \
    ${prefix:+--prefix "$prefix"} \
    ${force:+--force} \
    ${deinit_all:+--all} "[email protected]"


    Answered on December 17, 2018.
    Add Comment

    Remove a submodule you need to:

    • Delete the relevant section from the .gitmodules file.
    • Stage the .gitmodules changes git add .gitmodules
    • Delete the relevant section from .git/config.
    • Run git rm –cached path_to_submodule no trailing slash.
    • Run rm -rf .git/modules/path_to_submodule
    • Commit git commit -m “Removed submodule <name>”
    • Delete the now untracked submodule files
    rm -rf path_to_submodule


    Answered on December 17, 2018.
    Add Comment

    git, two commands will do:

    git rm the_submodule
    rm -rf .git/modules/the_submodule

    To define, the_submodule is the relative path of the submodule inside the project.

    For example,

    subdir/my_submodule if the submodule is inside a subdirectory subdir.

    git config -f .git/config --remove-section submodule.the_submodule 2> /dev/null


    Answered on December 17, 2018.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.