How to remove a Git submodule

Git submodules are a very powerful way to add libraries to your project. They are easy to add but there is no easy way to remove them.
I recently needed to remove one submodule from one of my project and run in some pitfalls to avoid here are my findings:

If you for example need to remove a submodule called SELibrary in your main git repository folder, do the following:

  • Open a terminal shell in you main git repository folder
  • Edit the .gitmodule file and find and remove these three lines from it:
    [submodule "SELibrary"]
    path = SELibrary
    url = https://SergioEstevao@github.com/SergioEstevao/SELibrary.git
  • Remove these two lines from .git/config
    [submodule "SELibrary"]
    url = https://SergioEstevao@github.com/SergioEstevao/SELibrary.git
  • Delete the git reference file that holds the submodule’s SHA commit id. Note the important lack of a trailing slash.
    git rm --cached SELibrary
  • Git will now see the entire directory as new files, because it’s no longer a submodule. Now you are free to delete the whole lot.
    rm -rf SELibrary/

If you ever need to do a move of a submodule remove it and then add the submodule in the new location.


Posted

in

by

Tags:

Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: