Bird
Raised Fist0
Gitdevops~7 mins

Removing submodules in Git - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you add a submodule to your project but later decide you no longer need it. Removing a submodule requires cleaning up several places in your project to avoid leftover files or broken references.
When a submodule is no longer needed in your project and you want to remove its code and references cleanly.
When you want to reduce your repository size by removing unused submodules.
When a submodule's source repository is deprecated or moved and you want to stop tracking it.
When you want to simplify your project structure by removing nested repositories.
When you accidentally added a submodule and want to undo that addition completely.
Commands
This command removes the submodule's entry from Git's internal configuration and unregisters it, preparing for full removal.
Terminal
git submodule deinit -f path/to/submodule
Expected OutputExpected
Cleared directory 'path/to/submodule'
→
-f - Force deinitialization even if the submodule has local changes
This removes the submodule's separate Git repository data stored inside the main repository's .git folder.
Terminal
rm -rf .git/modules/path/to/submodule
Expected OutputExpected
No output (command runs silently)
This removes the submodule directory from the working tree and stages the removal for commit.
Terminal
git rm -f path/to/submodule
Expected OutputExpected
rm 'path/to/submodule'
→
-f - Force removal even if there are local changes
This commits the removal of the submodule from the repository history.
Terminal
git commit -m "Remove submodule path/to/submodule"
Expected OutputExpected
[main abc1234] Remove submodule path/to/submodule 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 160000 path/to/submodule
This deletes any leftover submodule files from your working directory if they still exist.
Terminal
rm -rf path/to/submodule
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else, remember: removing a submodule requires unregistering it, deleting its Git data, removing its files, and committing these changes.

Common Mistakes
Deleting the submodule folder manually without running git submodule deinit and git rm
This leaves Git still tracking the submodule, causing errors and leftover references.
Always run git submodule deinit and git rm before deleting the folder.
Not removing the submodule's Git data inside .git/modules, causing stale data
Git keeps the submodule's repository data, which can cause confusion or conflicts later.
Remove the submodule's folder inside .git/modules manually after deinit.
Not committing the removal changes, so the submodule still appears in history
Without committing, the removal is not recorded and will reappear on next checkout or clone.
Commit the removal with a clear message after git rm.
Summary
Run git submodule deinit -f to unregister the submodule.
Delete the submodule's Git data inside .git/modules manually.
Use git rm -f to remove the submodule directory and stage the removal.
Commit the removal to record the change in the repository.
Delete any leftover submodule files from your working directory.

Practice

(1/5)
1. What is the first step to remove a Git submodule from your project?
easy
A. Commit the changes immediately
B. Delete the submodule folder manually
C. Unregister the submodule using git submodule deinit
D. Run git rm --cached on the main repository

Solution

  1. Step 1: Unregister the submodule

    Use git submodule deinit <path> to unregister the submodule from Git's configuration.
  2. Step 2: Remove submodule files and commit

    After unregistering, delete the submodule folder and commit the changes to keep the project clean.
  3. Final Answer:

    Unregister the submodule using git submodule deinit -> Option C
  4. Quick Check:

    First step is unregistering submodule [OK]
Hint: Always unregister submodule before deleting files [OK]
Common Mistakes:
  • Deleting folder before unregistering causes errors
  • Committing before unregistering submodule
  • Skipping unregister step
2. Which command correctly removes a submodule's cached entry from Git's index?
easy
A. git rm <submodule_path>
B. git delete submodule <submodule_path>
C. git submodule remove <submodule_path>
D. git rm --cached <submodule_path>

Solution

  1. Step 1: Remove cached submodule entry

    Use git rm --cached <submodule_path> to remove the submodule from Git's index without deleting files immediately.
  2. Step 2: Delete submodule folder and commit

    After removing cached entry, delete the folder manually and commit the changes.
  3. Final Answer:

    git rm --cached <submodule_path> -> Option D
  4. Quick Check:

    Remove cached entry with git rm --cached [OK]
Hint: Use --cached to remove from index but keep files temporarily [OK]
Common Mistakes:
  • Using git rm without --cached deletes files immediately
  • Using non-existent git submodule remove command
  • Trying git delete submodule which is invalid
3. Given these commands run in order, what is the final state of the submodule folder?
git submodule deinit mysub
rm -rf .git/modules/mysub
git rm --cached mysub
rm -rf mysub
git commit -m "Remove submodule"
medium
A. The submodule folder and Git data are fully removed
B. The submodule folder remains but is untracked
C. Only Git data is removed, folder remains
D. Submodule is still registered and tracked

Solution

  1. Step 1: Unregister and remove Git data

    git submodule deinit mysub unregisters the submodule, and rm -rf .git/modules/mysub deletes its Git data.
  2. Step 2: Remove cached index and folder, then commit

    git rm --cached mysub removes it from Git index, rm -rf mysub deletes the folder, and commit saves changes.
  3. Final Answer:

    The submodule folder and Git data are fully removed -> Option A
  4. Quick Check:

    All steps done means full removal [OK]
Hint: Deinit, remove Git data, rm cached, delete folder, commit [OK]
Common Mistakes:
  • Forgetting to delete .git/modules data
  • Not committing after removal
  • Deleting folder before unregistering
4. You ran rm -rf mysub before git submodule deinit mysub. What problem might occur?
medium
A. Git still tracks the submodule and shows errors on next fetch
B. The submodule folder is restored automatically
C. Git removes the submodule automatically without errors
D. No problem, this is the correct order

Solution

  1. Step 1: Understand order importance

    Deleting the folder before unregistering leaves Git still tracking the submodule path.
  2. Step 2: Resulting error state

    Git will show errors or warnings because the submodule is missing but still registered.
  3. Final Answer:

    Git still tracks the submodule and shows errors on next fetch -> Option A
  4. Quick Check:

    Delete folder before deinit causes tracking errors [OK]
Hint: Always deinit before deleting submodule folder [OK]
Common Mistakes:
  • Deleting folder first then deinit
  • Ignoring Git errors after removal
  • Assuming folder deletion unregisters submodule
5. You want to completely remove a submodule named libs/toolkit from your project. Which sequence of commands correctly removes it and cleans all related data?
hard
A. git rm libs/toolkit; rm -rf libs/toolkit; git commit -m "Remove submodule"
B. git submodule deinit libs/toolkit; rm -rf .git/modules/libs/toolkit; git rm --cached libs/toolkit; rm -rf libs/toolkit; git commit -m "Remove submodule"
C. rm -rf libs/toolkit; git submodule deinit libs/toolkit; git commit -m "Remove submodule"
D. git submodule remove libs/toolkit; git commit -m "Remove submodule"

Solution

  1. Step 1: Unregister and remove Git submodule data

    Run git submodule deinit libs/toolkit to unregister, then delete Git metadata with rm -rf .git/modules/libs/toolkit.
  2. Step 2: Remove cached index and folder, then commit

    Use git rm --cached libs/toolkit to remove from index, delete the folder manually, and commit changes.
  3. Final Answer:

    git submodule deinit libs/toolkit; rm -rf .git/modules/libs/toolkit; git rm --cached libs/toolkit; rm -rf libs/toolkit; git commit -m "Remove submodule" -> Option B
  4. Quick Check:

    Full removal requires unregister, delete Git data, rm cached, delete folder, commit [OK]
Hint: Deinit, delete .git/modules, rm cached, delete folder, commit [OK]
Common Mistakes:
  • Skipping removal of .git/modules data
  • Using non-existent git submodule remove command
  • Deleting folder before unregistering
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard