Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@giggio

This comment has been minimized.

Copy link

commented Jul 12, 2018

Works perfectly! Thanks!

@Anril

This comment has been minimized.

Copy link

commented Jul 17, 2018

Nice

@shahha

This comment has been minimized.

Copy link

commented Jul 18, 2018

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>

@schef

This comment has been minimized.

Copy link

commented Jul 19, 2018

Thanks.

@migueloop

This comment has been minimized.

Copy link

commented Jul 23, 2018

Thanks!

@hhendrikk

This comment has been minimized.

Copy link

commented Jul 24, 2018

Thanks!

@manasb-uoe

This comment has been minimized.

Copy link

commented Jul 24, 2018

Thanks!

@KuroNoDev

This comment has been minimized.

Copy link

commented Jul 26, 2018

Thanks!

@jimbojd72

This comment has been minimized.

Copy link

commented Jul 26, 2018

Refer to that 3 times today. Thanks

@codex-corp

This comment has been minimized.

@robin-rpr

This comment has been minimized.

Copy link

commented Aug 5, 2018

Thanks!

@ayham-hassan

This comment has been minimized.

Copy link

commented Aug 6, 2018

Thanks!

@usmanhafeez147

This comment has been minimized.

Copy link

commented Aug 11, 2018

thanks!

@lolgear

This comment has been minimized.

Copy link

commented Aug 12, 2018

And GitHub still hasn't upvote button :)

@dustycodes

This comment has been minimized.

Copy link

commented Aug 13, 2018

Perfect! Thank you!

@HenrySu

This comment has been minimized.

Copy link

commented Aug 15, 2018

Great

@dearaujoj

This comment has been minimized.

Copy link

commented Aug 16, 2018

Super :-)

@IdoZilberberg

This comment has been minimized.

Copy link

commented Aug 19, 2018

Works fine, thanks!

@Coeur

This comment has been minimized.

Copy link

commented Aug 20, 2018

This is a non-credited copy of https://stackoverflow.com/a/1260982/1033581. This gist is in violation of licence cc by-sa 3.0 with attribution required.

@RAStacy

This comment has been minimized.

Copy link

commented Aug 21, 2018

This has saved me so many times. Thanks!

@KimAndGumi

This comment has been minimized.

Copy link

commented Aug 29, 2018

Thanks

@tchakabam

This comment has been minimized.

Copy link

commented Aug 29, 2018

in modern git versions you can just git rm <path-to-submodule. it does all the dirty work for you. you can add the -f option in case there are new commits lingering or other unstaged or untracked things.

@maciej-gurban

This comment has been minimized.

Copy link

commented Sep 3, 2018

@tchakabam How new are we talking? I tried with 2.15.0 and git rm didn't remove it from .git/config but did remove it from .gitmodules and removed the submodule itself

@maciej-gurban

This comment has been minimized.

Copy link

commented Sep 3, 2018

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
@muhammedbaderdien

This comment has been minimized.

Copy link

commented Sep 12, 2018

Thanks a lot

@holgerbrandl

This comment has been minimized.

Copy link

commented Sep 23, 2018

awesome thanks!

@ccampanale

This comment has been minimized.

Copy link

commented Sep 24, 2018

👍 good stuff!

@sunt05

This comment has been minimized.

Copy link

commented Oct 3, 2018

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

It seems this one should be somehow voted-up.

Thanks a lot!

@abduakhatov

This comment has been minimized.

Copy link

commented Oct 4, 2018

magic

@Dev-Osmium

This comment has been minimized.

Copy link

commented Oct 8, 2018

Thank you so much!

@sfoley-gpqa

This comment has been minimized.

Copy link

commented Oct 18, 2018

Worked perfectly. Thank you very much

@Turim

This comment has been minimized.

Copy link

commented Oct 26, 2018

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

For my cause it turned out that 2nd line must be
rm -rf .git/modules/submodule_name

For instance , if path/to/submodule literally ./p/t/s then .git/modules could not contain the same hierarchy (depending on whether it was or wasn't specified the name option due to submodule add)

@JonathanMATechServices

This comment has been minimized.

Copy link

commented Oct 30, 2018

Shouldn't I delete the .git file in the submodule's folder? Assuming I want to keep the submodule's files?

@gupta-alok

This comment has been minimized.

Copy link

commented Nov 1, 2018

Nice link to follow for deleting the files gracefully from index and then re-add it as submodule.
https://stackoverflow.com/questions/12898278/issue-with-adding-common-code-as-git-submodule-already-exists-in-the-index

@the-nose-knows

This comment has been minimized.

Copy link

commented Dec 18, 2018

Thanks for the updated steps @shahha. Your steps worked like a charm compared to the original steps.

@kriansa

This comment has been minimized.

Copy link

commented Dec 30, 2018

I find this one faster (and easily scriptable):

submodule="folder/submodule"

git rm "$submodule"
rm -rf ".git/modules/$submodule"
git config -f ".git/config" --remove-section "submodule.$submodule" 2> /dev/null

# Commit the change
git commit -m "Remove submodule $submodule"

Credit: https://stackoverflow.com/a/21211232

@mcandre

This comment has been minimized.

Copy link

commented Dec 31, 2018

Thank you for posting!

WHY is this is not a single, built-in command in git itself??

@ice7mayu

This comment has been minimized.

Copy link

commented Jan 4, 2019

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>

works like a charm , thanks

@opyate

This comment has been minimized.

Copy link

commented Jan 25, 2019

@mdaizovi

This comment has been minimized.

Copy link

commented Jan 28, 2019

Thank you!

@xshirade

This comment has been minimized.

Copy link

commented Jan 31, 2019

Thank you.

@sarkarchandan

This comment has been minimized.

Copy link

commented Feb 5, 2019

Thanks for detailed steps. Works cleanly.

@moyaldror

This comment has been minimized.

Copy link

commented Feb 14, 2019

thanks!
created a gist with bash script for this - https://gist.github.com/moyaldror/63b4c2b601592aa3ae8a317adec00a1c

@ardalahmet

This comment has been minimized.

Copy link

commented Feb 15, 2019

Thanks!

@SteeleDynamics

This comment has been minimized.

Copy link

commented Mar 12, 2019

Thanks!

@rayway30419

This comment has been minimized.

Copy link

commented Mar 21, 2019

Thanks!!!!!

@kneerunjun

This comment has been minimized.

Copy link

commented Mar 23, 2019

Thats the cleanest way to dump a submodule, Accept my humble upvote sir!

@M4N0V3Y

This comment has been minimized.

Copy link

commented Mar 25, 2019

Great!

@Nobadeer

This comment has been minimized.

Copy link

commented Mar 28, 2019

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>

This worked perfectly. Thanks.

@obeninteractive

This comment has been minimized.

Copy link

commented May 5, 2019

thanks !

@vigntom

This comment has been minimized.

Copy link

commented May 8, 2019

Thanks !

@ducklin5

This comment has been minimized.

Copy link

commented May 11, 2019

Thanks!

@DevEddy

This comment has been minimized.

Copy link

commented May 29, 2019

Thanks

@lemasyma

This comment has been minimized.

Copy link

commented May 31, 2019

Works perfectly! Thank you

@monocasual

This comment has been minimized.

Copy link

commented Jun 7, 2019

<3

@fedorenchik

This comment has been minimized.

Copy link

commented Jun 11, 2019

<3

@esteban-gs

This comment has been minimized.

Copy link

commented Jun 14, 2019

thanks!

@Geylnu

This comment has been minimized.

Copy link

commented Jul 3, 2019

thanks!

@leviv

This comment has been minimized.

Copy link

commented Jul 7, 2019

Thank you!!

@harshsinghal

This comment has been minimized.

Copy link

commented Jul 7, 2019

Very helpful! Thanks for sharing.

@erkanzileli

This comment has been minimized.

Copy link

commented Jul 8, 2019

Works nice. Thanks.

@azemtsov

This comment has been minimized.

Copy link

commented Jul 11, 2019

Saved my day

@AndrewOfC

This comment has been minimized.

Copy link

commented Jul 15, 2019

Thank you very much for working out the nuts&bolts of how to cleanly remove a submodule and posting it for us. However, to my mind, it just gives me another reason to hate submodules. 😠

@trunneml

This comment has been minimized.

Copy link

commented Jul 16, 2019

Thanks

@fperezgamonal

This comment has been minimized.

Copy link

commented Jul 17, 2019

Thank you so much! 👐 I was stuck not understanding why I could not remove "just" by using rm -rf --cached path_to_submodule and it was because I had not purged the configuration files.

@LX314

This comment has been minimized.

Copy link

commented Jul 24, 2019

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>

works like a charm , thanks

nice one

@LX314

This comment has been minimized.

Copy link

commented Aug 1, 2019

this is my version

git rm --cached <path_to_submodule> rm -rf <path_to_submodule> git commit -m "Removed <path_to_submodule> submodule " rm -rf .git/modules/<path_to_submodule> git config -f .gitmodules --remove-section submodule.<path_to_submodule> git config -f .git/config --remove-section submodule.<path_to_submodule>

@LX314

This comment has been minimized.

Copy link

commented Aug 1, 2019

git rm --cached <path_to_submodule>
rm -rf <path_to_submodule>
git commit -m "Removed <path_to_submodule> submodule "
rm -rf .git/modules/<path_to_submodule>
git config -f .gitmodules --remove-section submodule.<path_to_submodule>
git config -f .git/config --remove-section submodule.<path_to_submodule>
@moeabdol

This comment has been minimized.

Copy link

commented Aug 9, 2019

Excellent!
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.