How to remove a git submodule

Ok, so we all know that having git submodules to include libraries in a git project is very handy. Today, I was trying to remove a git submodule that I had in a project, and couldn’t really a find a good answer by googling, so I went in irc and thanks to a good samaritan: Ilari, I found the answer.

Let’s say that you have a submodule in your project called ’submodule1′ and it’s in the following path: ‘vendors/submodule1′. In git there are 3 traces of this this submodule:
1) .gitmodules
2) .git/config
3) the submodule entry in the index/commit itself.

To remove the first two, is really simple, you just edit those files and remove the lines that specify the submdoule. In order to delete the third and last trace of the submodule in git, you need to type the following command:
git rm --cached path/to/submodule
Note: Do not put a trailing slash at the end of path. If you put a trailing slash at the end of the command, it will fail.

In the example above, we would type (do not include trailing slash after submodule1):
git rm --cached vendors/submodule1

Finding this information in google is not easy, hopefully this will help other git users.

Comments (25)

  1. Akhil Bansal wrote::

    Thanks,

    It was really very helpful.

    Akhil

    Monday, June 16, 2008 at 9:43 pm #
  2. kain wrote::

    doesn’t work for me:
    fatal: pathspec ‘vendor/plugins/will_paginate/’ did not match any files

    Tuesday, June 24, 2008 at 7:08 am #
  3. admin wrote::

    Akhil – I’m glad it was helpful. I was surprised that finding the answer to a simple question was not available in the docs :(

    Tuesday, June 24, 2008 at 12:12 pm #
  4. admin wrote::

    Kain – I created a smal scratch repo with a submodule, and after I typed the git rm –cached path/to/submodule command, the submodule is staged to be deleted in the index. You need to use “git commit” to commit the deletion of the submodule as well as the .gitmodules file.

    If you are getting the error when you type the git rm –cached command that means that your submodule is not tracked in the index itself. Check with git-status, and see if that submodule is set for deletion. If it isn’t, something went wrong while you were setting up your submodule.

    Tuesday, June 24, 2008 at 12:25 pm #
  5. Roadmaster wrote::

    kain:

    git rm –cached path/to/submodule/
    won’t work

    git rm –cached path/to/submodule
    this works.

    Friday, June 27, 2008 at 5:36 pm #
  6. Alex wrote::

    Thanks a million for this post. Hopefully this feature will be added to this git core sometime soon.

    Friday, August 15, 2008 at 9:04 am #
  7. Thomas the Git-flummoxed wrote::

    Thanks very much — worked for me.

    Friday, August 22, 2008 at 10:08 am #
  8. Pablo Castellazzi wrote::

    I was looking for an answer to this issue too, but in my experiments only *git rm* seems to be mandatory, if i understood right .gitmodules is some kind of database to keep track of the relation [ module path, git uri ], same for .git/config, you dont need to update them manually. Also *git rm* only remove the module from the git repository database, the files will persist unless you manually delete them.

    Please correct me if im wrong.

    Sunday, August 31, 2008 at 12:14 pm #
  9. Matt Darby wrote::

    You just saved the rest of my hair!

    Wednesday, September 17, 2008 at 10:23 am #
  10. Thanks for a great tip.

    I wonder why doesn’t Git include a command to remove a submodule. Something similar to git submodule add.

    Anyone knows a reason why this doesn’t exists?

    Wednesday, October 15, 2008 at 1:35 am #
  11. Millisami wrote::

    Thanks,
    made my day.

    Tuesday, November 4, 2008 at 9:58 am #
  12. Thank you! I have been to visit this page a few times. It is very helpful. I always search for ‘git remove submodule’ and find myself here.

    Friday, November 28, 2008 at 11:54 am #
  13. pyodor wrote::

    “Finding this information in google is not easy, hopefully this will help other git users.”

    I totally disagree, when I google it, I came to this page…hehehe..,Thanks man!

    Saturday, December 27, 2008 at 9:45 pm #
  14. Rafael Rosa Fu wrote::

    Hi,

    Thanks for the tip, very handy.

    Cheers,
    Rafael.

    Monday, February 2, 2009 at 3:13 pm #
  15. andy wrote::

    from a casual git-user

    thank you, oh thank you

    Monday, February 16, 2009 at 9:09 am #
  16. Mario Zigliotto wrote::

    Phew… thanks for this post. Was googling around a little bit and finding some very off topic stuff.

    Friday, May 1, 2009 at 6:38 pm #
  17. Moritz wrote::

    That is a useful tip. Thanks.

    Unfortunately, I still have a trace of a removed submodule: git submodule summary still shows remnants of its existence. Any ideas?

    Tuesday, June 9, 2009 at 5:25 pm #
  18. Moritz wrote::

    Jose, can you please remove the last comment that exposes the email. Thanks.

    Tuesday, June 9, 2009 at 10:39 pm #
  19. floppyears wrote::

    I removed the email from your last comment before it was approved.

    Tuesday, June 9, 2009 at 10:49 pm #
  20. Amiel Martin wrote::

    thanks to you, it is now easy to find on google.

    thanks

    Friday, June 12, 2009 at 11:38 am #
  21. Joff Williams wrote::

    I’ve come back to this twice already! Thank you.

    Thursday, July 16, 2009 at 5:05 am #
  22. yaman wrote::

    You’re a life saver. I’ve been dealing with a stubborn submodule for over an hour. This worked perfectly. Thanks for sharing.

    Monday, August 3, 2009 at 2:08 pm #
  23. Zashkaser wrote::

    Ya put a smile in my face.

    Wednesday, August 5, 2009 at 9:45 am #
  24. cheers very helpful thanx

    Wednesday, August 26, 2009 at 12:44 pm #
  25. Christoph wrote::

    Exactly what i was looking for. Thank you, worked like a charm!

    Friday, October 2, 2009 at 3:13 am #

Trackbacks/Pingbacks (5)

  1. Git Submodule Links - Software Engineering Blog on Tuesday, September 16, 2008 at 2:20 pm

    [...] isn’t a command to remove submodules from Git. If you need to remove them follow this post: http://pitupepito.homelinux.org/?p=24 NOTE: When doing ‘git rm –cached path/to/submodule’ do NOT include a trailing [...]

  2. fallinglab » Remover un submodulo git on Thursday, November 27, 2008 at 4:42 pm

    [...] Encontré la respuesta que transcribo a continuación en éste artículo [...]

  3. Aral Balkan - Links for 2008-12-21 on Sunday, December 21, 2008 at 4:03 pm

    [...] How to remove a git submodule < Flop’s Scribbles One way (?) to remove submodules in Git. (tags: git remove submodule) The Links for 2008-12-21 article by Aral Balkan, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial 2.0 UK: England License. [...]

  4. git submodule rm « Paul vs. Rails on Tuesday, June 23, 2009 at 12:57 pm

    [...] submodule rm Came across Jose Cedeon’s blog post about how to remove a git submodule. Very [...]

  5. links for 2009-08-23 | NeXt on Sunday, August 23, 2009 at 7:38 am

    [...] How to remove a git submodule < Flop’s Scribbles (tags: programming tutorial development tips git submodule) Подобни постове:links for 2008-09-06links for 2009-05-17links for 2008-09-10links for 2008-09-07links for 2008-08-29 [...]