Skip to content

Instantly share code, notes, and snippets.

@myusuf3
Created November 3, 2014 17:36
Show Gist options
  • Save myusuf3/7f645819ded92bda6677 to your computer and use it in GitHub Desktop.
Save myusuf3/7f645819ded92bda6677 to your computer and use it in GitHub Desktop.
How effectively delete a git submodule.

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m "Removed submodule "
  • Delete the now untracked submodule files rm -rf path_to_submodule
@Icy-Thought
Copy link

Icy-Thought commented Mar 22, 2022

@Icy-Thought that's curious, what is your magit version? mine is Magit v3.3.0-199-g036f5cb7, Git 2.35.1, Emacs 27.1, gnu/linux

Magit 4e29d582, Git 2.35.1, Emacs 28.0.91, gnu/linux (using doom-emacs)

@Godsmith
Copy link

Godsmith commented Apr 6, 2022

FYI, removing a git submodule is much simpler now with a modern Git version in 2022. Just run git rm <path-to-submodule> and commit.

Taken from here: https://stackoverflow.com/questions/1260748/how-do-i-remove-a-submodule

@min-hinthar
Copy link

It works. Thanks for the share! Have fun coding.

@RecoX
Copy link

RecoX commented May 23, 2022

This seems to be a more modern version (copied from https://stackoverflow.com/a/36593218/2066118):

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule

We should upvote this answer

Done!

@psytron
Copy link

psytron commented May 27, 2022

I think above can be simplified using following commands.
git submodule deinit <path_to_submodule>
git rm <path_to_submodule>
git commit-m "Removed submodule "
rm -rf .git/modules/<path_to_submodule>

true works efficiently. Thnks

Excellent!

@slo-addy
Copy link

slo-addy commented Jun 2, 2022

Life saver! Thank you.

@jairusjoer
Copy link

Thanks 🔥

@coeneivan
Copy link

👍

@panozzaj
Copy link

Building on the answers above...

If you're doing this a lot, you can create an executable script discoverable in your PATH named git-submodule-remove with the following contents: https://github.com/panozzaj/conf/blob/master/common/bin/git-submodule-remove

Then you can run git submodule-remove path/to/submodule to quickly remove it.

@Sasino97
Copy link

rm -rf is Linux/UNIX only, you shouldn't assume that we're all using Linux

@dptsolutions
Copy link

@Sasino97 - If you're not on a *NIX environment you are likely on Windows then, and you can use my Powershell variant I shared earlier in this thread.

@stevenbarragan
Copy link

Thank you!

@JSAssassin
Copy link

Thank you. This is really helpful.

@barrientosvctor
Copy link

This gist is very helpful, thanks a lot!

@johannes-steurer
Copy link

Just perfect. Thank you

@michaelmontero
Copy link

amazing

@jonlowrey
Copy link

Thank You!

@elquchiri
Copy link

This seems to be a more modern version (copied from https://stackoverflow.com/a/36593218/2066118):

# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule

We should upvote this answer

For those of us on Windows, here's the equivalent in Powershell:

# Remove the submodule entry from .git/config
git submodule deinit -f path\to\submodule

# Remove the submodule directory from the superproject's .git/modules directory
rm .git\modules\path\to\submodule -r -fo

# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path\to\submodule

Good response, Thank's man.

@cgair
Copy link

cgair commented Mar 14, 2023

Every time I want to remove a submodule, I come back to this Gist and follow the instructions. It's crazy how difficult it is to work with submodule. Many thanks!

  • 11111111111111111....

@CharFractal
Copy link

thank you so much

@nvcastet
Copy link

Thank you!

@harlesty
Copy link

If you're doing this a lot, you can create an executable script discoverable in your PATH named git-submodule-remove with the following contents: https://github.com/panozzaj/conf/blob/master/common/bin/git-submodule-remove

Then you can run git submodule-remove path/to/submodule to quickly remove it and play uno online card game
Good response, Thank's man.

@derzhavets
Copy link

Thank you

@andres-pcg
Copy link

I was using this gist for a while. It was really helpful!

However, deleting a submodule it’s a lot easier nowadays. Latest GitHub versions allows you to run: git rm <path_to_submodule> and git takes care of it. Afterwards you just need to commit the changes and that’s it.

I hope this helps.

@AdamZWinter
Copy link

Got an error from the "git rm --cached path_to_submodule" part. (something about sparse checkout)

Fixed it by running this intead:
"git update-index --remove path_to_submodule"

@wisekeep
Copy link

Obrigado!

@naomiiiiiiiii
Copy link

Thanks!

@MeronGit
Copy link

MeronGit commented Feb 1, 2024

Seemed to work well :)

@Dynesshely
Copy link

Suggestion: we can run command git config -f .gitmodules --remove-section "submodule.<submodule_name>"

@gmsotavio
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment