Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a git submodule locally?

I want to delete a submodule locally without pushing the changes to mainline.

Here is what I have done:

https://gist.github.com/myusuf3/7f645819ded92bda6677

// Remove the submodule entry from .git/config

$ git submodule deinit -f my_submodule

// Remove the submodule directory from the superproject's .git/modules directory

$ rm -rf .git/modules/my_submodule

// Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule

$ git rm -f my_submodule

Now here is what I have now:

$ git status -s -uno
D  my_submodule

Question> If I commit this change locally and later push all my local changes to main, will these commands cause the submodule(i.e. my_submodule) deleted from mainline?

Thank you

like image 329
q0987 Avatar asked Oct 31 '25 10:10

q0987


1 Answers

You were doing great up until git rm -f my_submodule: this command will cause a git status change (as you have seen).

When you want to delete a submodule only locally without risking pushing this removal to your remote, e.g. when you previously instantiated your git submodules using git submodule init ... and now want to get rid of them and the diskspace they consume, while still tracking them in your git repo as before, then all you need are the first two git commands you specified:

git submodule deinit -f my_submodule

or

git submodule deinit --all -f

if you want to get rid of all your instantiated submodules all at once.

plus removing the local submodule git database:

rm -rf .git/modules/my_submodule

Note that

git status

after these two commands will still produce an empty change set, as we intended: git now still knows about the submodules (via .gitsubmodules) but doesn't take up diskspace for their local instantiations any more.

If you change your mind any time later, you can always run a new git submodule init command and have them back at their .gitmodule-designated paths.

like image 200
Ger Hobbelt Avatar answered Nov 02 '25 01:11

Ger Hobbelt