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.

giggio commented Jul 12, 2018

Works perfectly! Thanks!

@Anril

This comment has been minimized.

Anril commented Jul 17, 2018

Nice

@shahha

This comment has been minimized.

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.

schef commented Jul 19, 2018

Thanks.

@migueloop

This comment has been minimized.

migueloop commented Jul 23, 2018

Thanks!

@0v3lh4

This comment has been minimized.

0v3lh4 commented Jul 24, 2018

Thanks!

@manasb-uoe

This comment has been minimized.

manasb-uoe commented Jul 24, 2018

Thanks!

@KuroNoDev

This comment has been minimized.

KuroNoDev commented Jul 26, 2018

Thanks!

@jimbojd72

This comment has been minimized.

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.

robin-rpr commented Aug 5, 2018

Thanks!

@ayham-hassan

This comment has been minimized.

ayham-hassan commented Aug 6, 2018

Thanks!

@usmanhafeez147

This comment has been minimized.

usmanhafeez147 commented Aug 11, 2018

thanks!

@lolgear

This comment has been minimized.

lolgear commented Aug 12, 2018

And GitHub still hasn't upvote button :)

@dustycodes

This comment has been minimized.

dustycodes commented Aug 13, 2018

Perfect! Thank you!

@HenrySu

This comment has been minimized.

HenrySu commented Aug 15, 2018

Great

@dearaujoj

This comment has been minimized.

dearaujoj commented Aug 16, 2018

Super :-)

@IdoZilberberg

This comment has been minimized.

IdoZilberberg commented Aug 19, 2018

Works fine, thanks!

@Coeur

This comment has been minimized.

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.

RAStacy commented Aug 21, 2018

This has saved me so many times. Thanks!

@KimAndGumi

This comment has been minimized.

KimAndGumi commented Aug 29, 2018

Thanks

@tchakabam

This comment has been minimized.

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.

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.

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.

muhammedbaderdien commented Sep 12, 2018

Thanks a lot

@holgerbrandl

This comment has been minimized.

holgerbrandl commented Sep 23, 2018

awesome thanks!

@ccampanale

This comment has been minimized.

ccampanale commented Sep 24, 2018

👍 good stuff!

@sunt05

This comment has been minimized.

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.

abduakhatov commented Oct 4, 2018

magic

@Dev-Osmium

This comment has been minimized.

Dev-Osmium commented Oct 8, 2018

Thank you so much!

@sfoley-gpqa

This comment has been minimized.

sfoley-gpqa commented Oct 18, 2018

Worked perfectly. Thank you very much

@Turim

This comment has been minimized.

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.

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.

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

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