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!

@0v3lh4

This comment has been minimized.

Copy link

0v3lh4 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!

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