Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the current way to remove a git submodule?

as of git version 1.9.3 (Apple Git-50) on mac how do i remove a git submodule? I am reading alot of outdated information with many developers telling me they wont work. What is the current way ? will git deinit pathToSubModule do the trick ?

The steps i thought would work are here but comments say they wont.

Let me explain my current situation and what i need accomplished. I've installed the Quick repository and added it to as submodule to my project. This code is already checked in and others are using it. What i now need to do is fork the same Quick repository and host it on a more secure github that my company has (so a completely other private github). After forking it i want to add that fork as a gitSubmodule and let it replace the current Quick submodule i had installed previously.

update: i've read that the following is the correct way on latest git version please confirm?

To remove a submodule added using:

git submodule add [email protected]:repos/blah.git lib/blah
Run:

git rm lib/blah
That's it.

For old versions of git (circa ~1.8.5) use:

git submodule deinit lib/blah
git rm lib/blah
git config -f .gitmodules --remove-section submodule.lib/blah
like image 435
j2emanue Avatar asked Apr 24 '15 14:04

j2emanue


People also ask

Why you should not use git submodules?

This is because of some major drawbacks around git submodules, such as being locked to a specific version of the outer repo, the lacking of effective merge management, and the general notion that the Git repository itself doesn't really know it's now a multi-module repository.

Will git pull update submodules?

With Git 2.34, if the repository is cloned with the --recurse-submodules , a simple git pull will recurse into submodules. No more git config --global submodule.

Does git clone pull submodules?

It automatically pulls in the submodule data assuming you have already added the submodules to the parent project. Note that --recurse-submodules and --recursive are equivalent aliases.


2 Answers

You have the git submodule deinit

git submodule deinit <asubmodule>    
git rm <asubmodule>
# Note: asubmodule (no trailing slash)
# or, if you want to leave it in your working tree
git rm --cached <asubmodule>
rm -rf .git/modules/<asubmodule>

deinit

Un-register the given submodules, i.e. remove the whole submodule.$name
section from .git/config together with their work tree.

Further calls to git submodule update, git submodule foreach and git submodule sync will skip any unregistered submodules until they are initialized again, so use this command if you don’t want to have a local checkout of the submodule in your work tree anymore.

If you really want to remove a submodule from the repository and commit that use git rm instead.

If --force is specified, the submodule’s work tree will be removed even if it contains local modifications.

like image 132
CodeWizard Avatar answered Oct 11 '22 06:10

CodeWizard


I'm using Git version 2.16.2 and git rm does the job mostly well:

git rm path-to-submodule

You can verify with git status and git diff --cached that this deinitializes the submodule and modifies .gitmodules automatically. As always, you need to commit the change.

However, even though the submodule is removed from source control, .git/modules/path-to-submodule still contains the submodule repository and .git/config contains its URL, so you still have to remove those manually:

git config --remove-section submodule.path-to-submodule
rm -rf .git/modules/path-to-submodule

Keeping the submodule repository and configuration is intentional so that you can undo the removal with e.g. git reset --hard.

like image 14
mrts Avatar answered Oct 11 '22 07:10

mrts