Make your Git repo cleaner — subscribers get coupons, early access to new Git guides, and exclusive YouTube videos.
Subscribe to Learning Ocean · My YouTube Channel · Video: <VIDEO_URL>

Remove a Git Submodule Safely — Complete Step-by-Step Guide

In the previous article we learned how to add, update, and sync submodules.
Now let’s cleanly remove a submodule so no hidden data remains.


1️⃣ De-initialize the submodule

First remove the submodule’s section from .git/config:

git submodule deinit hooks

This deletes the [submodule "hooks"] section inside .git/config.

🔎 Check with:

cat .git/config

2️⃣ Remove entry from .gitmodules

Open .gitmodules and delete the block for the submodule you’re removing:

git rm --cached .gitmodules   # optional if you remove all entries
# or edit .gitmodules manually and stage the change:
git add .gitmodules

Typical block to delete:

[submodule "hooks"]
    path = hooks
    url = git@github.com:user/hooks.git

Commit this change:

git commit -m "remove hooks entry from .gitmodules"

3️⃣ Delete the submodule directory

Remove the actual folder and tell Git that it is gone:

git rm -f hooks
git commit -m "remove hooks directory"

Now the working tree no longer contains the submodule.


4️⃣ Clean leftover data in .git/modules

Git keeps a hidden clone of every submodule inside .git/modules/. Delete its directory to free space:

rm -rf .git/modules/hooks

This step ensures no stray objects remain in the repository.


5️⃣ Push changes

Finally push to the remote repository:

git push origin main

Now anyone who clones the project will no longer see the removed submodule.


Quick Recap of All Commands

git submodule deinit hooks
vi .gitmodules          # remove hooks section, or edit with your editor
git add .gitmodules
git commit -m "remove hooks entry"

git rm -f hooks
git commit -m "remove hooks directory"

rm -rf .git/modules/hooks

git push origin main

Visual Summary



MCQs — Test Your Knowledge

1) What does git submodule deinit <name> do? A) Deletes the folder from the working tree B) Removes its entry from .git/config C) Deletes the hidden clone in .git/modules D) Pushes all changes to origin Answer: B


2) Where does Git keep a hidden clone of every submodule? A) Inside .git/submodules B) Inside .git/modules C) Inside .git/objects D) Only in the working tree Answer: B


3) Why must you remove .git/modules/<submodule> after deleting a submodule? A) To free disk space and prevent stale data B) To enable git rebase C) To change remote URL D) To clean the commit history Answer: A


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉