103

as of git version 1.9.3 (Apple Git-50) on mac how do i remove a git submodule? I am reading alot of outdated information with many developers telling me they wont work. What is the current way ? will git deinit pathToSubModule do the trick ?

The steps i thought would work are here but comments say they wont.

Let me explain my current situation and what i need accomplished. I've installed the Quick repository and added it to as submodule to my project. This code is already checked in and others are using it. What i now need to do is fork the same Quick repository and host it on a more secure github that my company has (so a completely other private github). After forking it i want to add that fork as a gitSubmodule and let it replace the current Quick submodule i had installed previously.

update: i've read that the following is the correct way on latest git version please confirm?

To remove a submodule added using:

git submodule add blah@blah.com:repos/blah.git lib/blah
Run:

git rm lib/blah
That's it.

For old versions of git (circa ~1.8.5) use:

git submodule deinit lib/blah
git rm lib/blah
git config -f .gitmodules --remove-section submodule.lib/blah
xpt
  • 20,363
  • 37
  • 127
  • 216
j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • I've wrestled submodule problems for two days. The breakthrough came when I found this: https://forums.developer.apple.com/thread/13102. Basically, Xcode, and perhaps other apps, struggle to expand url's containing '~'. Once I changed ssh://username@server.remoteHost.com/~/git/MyRepo.git to ssh://username@server.remoteHost.com/home/username/git/MyRepo.git (look up the actual path on your server), all the weirdness disappeared with ten minutes. – Elise van Looij Feb 09 '18 at 12:50

3 Answers3

220

You have the git submodule deinit

git submodule deinit <asubmodule>    
git rm <asubmodule>
# Note: asubmodule (no trailing slash)
# or, if you want to leave it in your working tree
git rm --cached <asubmodule>
rm -rf .git/modules/<asubmodule>

deinit

Un-register the given submodules, i.e. remove the whole submodule.$name
section from .git/config together with their work tree.

Further calls to git submodule update, git submodule foreach and git submodule sync will skip any unregistered submodules until they are initialized again, so use this command if you don’t want to have a local checkout of the submodule in your work tree anymore.

If you really want to remove a submodule from the repository and commit that use git rm instead.

If --force is specified, the submodule’s work tree will be removed even if it contains local modifications.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 9
    i want to completely wipe out the submodule for me and the other users as well. So all i need is git submodule deinit asubmodule; and git rm asubmodule ?? Also what about the other users cached submodule, would they have to delete it after pulling down ? – j2emanue Apr 24 '15 at 14:55
  • 1
    I wanted to delete the local copy of the submodule and this worked perfect, all the files in the subdir are gone. – malhal Sep 03 '16 at 12:33
  • 1
    You may also need `rm -rf ` if you've already checked out a version of the base repository which has un-registered the submodule. – Sarah Messer Oct 04 '16 at 22:40
15

I'm using Git version 2.16.2 and git rm does the job mostly well:

git rm path-to-submodule

You can verify with git status and git diff --cached that this deinitializes the submodule and modifies .gitmodules automatically. As always, you need to commit the change.

However, even though the submodule is removed from source control, .git/modules/path-to-submodule still contains the submodule repository and .git/config contains its URL, so you still have to remove those manually:

git config --remove-section submodule.path-to-submodule
rm -rf .git/modules/path-to-submodule

Keeping the submodule repository and configuration is intentional so that you can undo the removal with e.g. git reset --hard.

mrts
  • 16,697
  • 8
  • 89
  • 72
3

How to safely remove a submodule.

(adds to https://stackoverflow.com/a/1260982/342794)

  1. List and locate the submodule section in .gitmodules file. Say via terminal vi .gitmodules. Example:

    [submodule "submodules/afnetworking"]
        path = submodules/afnetworking
        url = https://github.com/CompanyName/afnetworking.git
    
  2. Submodules can be used with local forks. For a long running projects, code may differ from original repo and might have some fixes. It's a good idea to verify if submodule code went through changes over the course of project. Example: Look at the logs, if it's pointing to master branch or some local branch. exploring git logs to find modifications that were done via terminal. typically these are stored under a directory, my example, under submodules directory.

    User$ cd submodules/afnetworking
    User$ git log
    
  3. Remove the submodule section from .gitmodules, save the file. Example: git status should show only following as modified

    modified:   .gitmodules
    
  4. Stage the changes with git add .gitmodules.

  5. List and locate the submodule section in .git/config files. Say via terminal vi .git/config. Example:

    [submodule "submodules/afnetworking"]
        url = https://github.com/CompanyName/afnetworking.git
    
  6. Remove the git cache sobmodule files. running git rm --cached submodules/afnetworking (no trailing slash) would remove it successfully. Example:

    User$ git rm --cached submodules/afnetworking
    rm 'submodules/afnetworking'    <-- terminal output
    
  7. Remove the submodule files under .git directory with rm -rf .git/modules/.... Confirm it with git status afterwards.

    User$ rm -rf .git/modules/submodules/afnetworking/
    User$ git status
    On branch feature/replace-afnetworking-submodule-with-pod
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
        modified:   .gitmodules
        deleted:    submodules/afnetworking
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        submodules/afnetworking/
    
  8. Commit the changes. Confirm with git status afterwards.

    User$ git commit -m "Remove submodule afnetworking"
    [feature/replace-afnetworking-submodule-with-pod 70e239222] Remove submodule afnetworking
    2 files changed, 4 deletions(-)
      delete mode 160000 submodules/afnetworking
    User$ git status
    On branch feature/replace-afnetworking-submodule-with-pod
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        submodules/afnetworking/
    nothing added to commit but untracked files present (use "git add" to track)
    
  9. Delete the untracked submodule files.

    User$ rm -rf submodules/afnetworking
    
  10. Delete the references from Xcode project. Build, fix compile and runtime issues.

Michael
  • 8,362
  • 6
  • 61
  • 88
lal
  • 7,410
  • 7
  • 34
  • 45