Remove a Submodule within git

By  on  

For many git-based projects, submodules are useful in avoiding duplicate work and easing utility library updates.  There are times, however, when a submodule needs to be removed from a project.  Submodules aren't removed with git rm submoduledir, they must be removed in a more tedious, manual fashion.  There are many unclear explanations of how to remove a submodule but I found one on Stack Overflow that's concise, so I thought I'd share it.  The steps are as follows:

  1. Delete the relevant section from the .gitmodules file.  The section would look similar to:
    [submodule "vendor"]
    	path = vendor
    	url = git://github.com/some-user/some-repo.git
    
  2. Stage the .gitmodules changes via command line using:git add .gitmodules
  3. Delete the relevant section from .git/config, which will look like:
    [submodule "vendor"]
    	url = git://github.com/some-user/some-repo.git
    
  4. Run git rm --cached path/to/submodule .  Don't include a trailing slash -- that will lead to an error.
  5. Run rm -rf .git/modules/submodule_name
  6. Commit the change:
  7. Delete the now untracked submodule files rm -rf path/to/submodule

Those steps will get you rid of that unwanted submodule.  A lot harder than adding one, eh?

Recent Features

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    MooTools 1.2 Tooltips: Customize Your Tips

    I've never met a person that is "ehhhh" about XHTML/javascript tooltips; people seem to love them or hate them. I'm on the love side of things. Tooltips give you a bit more information about something than just the element itself (usually...

  • By
    Editable Content Using MooTools 1.2, PHP, and MySQL

    Everybody and their aerobics instructor wants to be able to edit their own website these days. And why wouldn't they? I mean, they have a $500 budget, no HTML/CSS experience, and extraordinary expectations. Enough ranting though. Having a website that allows for...

Discussion

  1. Just an interesting side note, as of git 1.8.3, you can use git submodule deinit to handle a lot of the heavy lifting of removing a submodule.

    • Sam

      deinit didn’t work for me; these instructions did (as of git 1.9.3)

  2. Funny, I had this problem just yesterday! Thanks anyway, I’ll remember where the manual is)

  3. I found a handy bash script that automates this https://gist.github.com/sharplet/6289697

    Change to a directory that’s in your PATH, I used /usr/local/bin and run the following commands:

    $ curl -o git-remove-submodule https://gist.github.com/sharplet/6289697/raw/git-remove-submodule

    $ chmod 755 git-remove-submodule

    Then to remove a submodule run:

    $ git remove-submodule path/to/submodule

  4. Joe

    These instructions are no longer current, I wouldn’t use them.

  5. Kurt

    This should NOT be used… ‘git rm path/to/submodule’ will work fine on recent git versions.

  6. Brian Berneker

    Thank you so much for this!

    I have a project that I am deploying to AWS, and while I want to keep sub folder repos able to push and pull, I don’t want the parent repo to treat them as submodules, because AWS doesn’t init and pull submodules, resulting in empty submodule folders on deployment.

    By using your steps here I was able to keep the files and still keep the folder contents in my repo.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!