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

giggio commented Jul 12, 2018

Works perfectly! Thanks!

@anril

This comment has been minimized.

Copy link

anril commented Jul 17, 2018

Nice

@shahha

This comment has been minimized.

Copy link

shahha 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

schef commented Jul 19, 2018

Thanks.

@migueloop

This comment has been minimized.

Copy link

migueloop commented Jul 23, 2018

Thanks!

@hhendrikk

This comment has been minimized.

Copy link

hhendrikk commented Jul 24, 2018

Thanks!

@manasb-uoe

This comment has been minimized.

Copy link

manasb-uoe commented Jul 24, 2018

Thanks!

@KuroNoDev

This comment has been minimized.

Copy link

KuroNoDev commented Jul 26, 2018

Thanks!

@jimbojd72

This comment has been minimized.

Copy link

jimbojd72 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

robin-rpr commented Aug 5, 2018

Thanks!

@ayham-hassan

This comment has been minimized.

Copy link

ayham-hassan commented Aug 6, 2018

Thanks!

@usmanhafeez147

This comment has been minimized.

Copy link

usmanhafeez147 commented Aug 11, 2018

thanks!

@lolgear

This comment has been minimized.

Copy link

lolgear commented Aug 12, 2018

And GitHub still hasn't upvote button :)

@dustycodes

This comment has been minimized.

Copy link

dustycodes commented Aug 13, 2018

Perfect! Thank you!

@HenrySu

This comment has been minimized.

Copy link

HenrySu commented Aug 15, 2018

Great

@dearaujoj

This comment has been minimized.

Copy link

dearaujoj commented Aug 16, 2018

Super :-)

@IdoZilberberg

This comment has been minimized.

Copy link

IdoZilberberg commented Aug 19, 2018

Works fine, thanks!

@Coeur

This comment has been minimized.

Copy link

Coeur 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

RAStacy commented Aug 21, 2018

This has saved me so many times. Thanks!

@KimAndGumi

This comment has been minimized.

Copy link

KimAndGumi commented Aug 29, 2018

Thanks

@tchakabam

This comment has been minimized.

Copy link

tchakabam 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

maciej-gurban 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

maciej-gurban 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

muhammedbaderdien commented Sep 12, 2018

Thanks a lot

@holgerbrandl

This comment has been minimized.

Copy link

holgerbrandl commented Sep 23, 2018

awesome thanks!

@ccampanale

This comment has been minimized.

Copy link

ccampanale commented Sep 24, 2018

👍 good stuff!

@sunt05

This comment has been minimized.

Copy link

sunt05 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

abduakhatov commented Oct 4, 2018

magic

@Dev-Osmium

This comment has been minimized.

Copy link

Dev-Osmium commented Oct 8, 2018

Thank you so much!

@sfoley-gpqa

This comment has been minimized.

Copy link

sfoley-gpqa commented Oct 18, 2018

Worked perfectly. Thank you very much

@Turim

This comment has been minimized.

Copy link

Turim 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

JonathanMATechServices 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

gupta-alok 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

the-nose-knows 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

kriansa 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

mcandre 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

ice7mayu 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

opyate commented Jan 25, 2019

@mdaizovi

This comment has been minimized.

Copy link

mdaizovi commented Jan 28, 2019

Thank you!

@xshirade

This comment has been minimized.

Copy link

xshirade commented Jan 31, 2019

Thank you.

@sarkarchandan

This comment has been minimized.

Copy link

sarkarchandan commented Feb 5, 2019

Thanks for detailed steps. Works cleanly.

@moyaldror

This comment has been minimized.

Copy link

moyaldror 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

ardalahmet commented Feb 15, 2019

Thanks!

@SteeleDynamics

This comment has been minimized.

Copy link

SteeleDynamics commented Mar 12, 2019

Thanks!

@rayway30419

This comment has been minimized.

Copy link

rayway30419 commented Mar 21, 2019

Thanks!!!!!

@kneerunjun

This comment has been minimized.

Copy link

kneerunjun 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

M4N0V3Y commented Mar 25, 2019

Great!

@Nobadeer

This comment has been minimized.

Copy link

Nobadeer 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

obeninteractive commented May 5, 2019

thanks !

@vigntom

This comment has been minimized.

Copy link

vigntom commented May 8, 2019

Thanks !

@ducklin5

This comment has been minimized.

Copy link

ducklin5 commented May 11, 2019

Thanks!

@DevEddy

This comment has been minimized.

Copy link

DevEddy commented May 29, 2019

Thanks

@lemasyma

This comment has been minimized.

Copy link

lemasyma commented May 31, 2019

Works perfectly! Thank you

@monocasual

This comment has been minimized.

Copy link

monocasual commented Jun 7, 2019

<3

@fedorenchik

This comment has been minimized.

Copy link

fedorenchik commented Jun 11, 2019

<3

@esteban-gs

This comment has been minimized.

Copy link

esteban-gs commented Jun 14, 2019

thanks!

@Geylnu

This comment has been minimized.

Copy link

Geylnu commented Jul 3, 2019

thanks!

@leviv

This comment has been minimized.

Copy link

leviv commented Jul 7, 2019

Thank you!!

@harshsinghal

This comment has been minimized.

Copy link

harshsinghal commented Jul 7, 2019

Very helpful! Thanks for sharing.

@erkanzileli

This comment has been minimized.

Copy link

erkanzileli commented Jul 8, 2019

Works nice. Thanks.

@azemtsov

This comment has been minimized.

Copy link

azemtsov commented Jul 11, 2019

Saved my day

@AndrewOfC

This comment has been minimized.

Copy link

AndrewOfC 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. 😠

@fperezgamonal

This comment has been minimized.

Copy link

fperezgamonal 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

LX314 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

LX314 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

LX314 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

moeabdol commented Aug 9, 2019

Excellent!
Thanks.

@franckXu

This comment has been minimized.

Copy link

franckXu commented Sep 19, 2019

thanks

@okovalov

This comment has been minimized.

Copy link

okovalov commented Oct 10, 2019

thank you!

@cgunnels

This comment has been minimized.

Copy link

cgunnels commented Oct 18, 2019

This should be on stackoverflow so I can upvote your answer...thanks!

@puremourning

This comment has been minimized.

Copy link

puremourning commented Oct 19, 2019

From git help submodules:


       o   Deleted submodule: A submodule can be deleted by running git rm <submodule path> && git commit. This can
           be undone using git revert.

           The deletion removes the superproject's tracking data, which are both the gitlink entry and the section
           in the .gitmodules file. The submodule's working directory is removed from the file system, but the Git
           directory is kept around as it to make it possible to checkout past commits without requiring fetching
           from another repository.

           To completely remove a submodule, manually delete $GIT_DIR/modules/<name>/.
@Daniel-Chin

This comment has been minimized.

Copy link

Daniel-Chin commented Nov 9, 2019

Thanks!

@nicenemo

This comment has been minimized.

Copy link

nicenemo commented Nov 11, 2019

Thanks!

@raystudio9236

This comment has been minimized.

Copy link

raystudio9236 commented Nov 22, 2019

Thanks!

@aitbaali

This comment has been minimized.

Copy link

aitbaali commented Nov 22, 2019

very helpful

@jmegevand

This comment has been minimized.

Copy link

jmegevand commented Nov 25, 2019

Thanks !!

@Paxa

This comment has been minimized.

Copy link

Paxa commented Nov 26, 2019

Thanks 👍

@levmichael3

This comment has been minimized.

Copy link

levmichael3 commented Nov 27, 2019

Thanks!! works like a charm! 😸

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.