How to make an existing directory within a git repository a git submodule

Use git submodule absorbgitdirs

This is what the docs state this command does:

If a git directory of a submodule is inside the submodule, move the git directory of the submodule into its superprojects $GIT_DIR/modules path and then connect the git directory and its working directory by setting the core.worktree and adding a .git file pointing to the git directory embedded in the superprojects git directory.

So instead of starting all over as suggested in the previous answers by @DomQ and myself, one can just add run the following:

  1. Without removing from the index the submodule, Add the submodule's url to .gitmodules and to .git/config with
    git submodule add <url> <path>
  2. Move the submodule's $GIT_DIR directory (.git in regular repositories) to .git/modules/<path> with
    git submodule absorbgitdirs <path>

Original answer - pre v2.12.0

git submodule absorbgitdirs was introduced only in v2.12.0-rc0 (see commit).

The Solution is quite simple. It was extracted from here.

  1. git rm submodule-dir
    This will delete all the files that git was tracking after in submodule-dir
  2. rm -rf submoduledir
    This will delete all the other files that might have been left in submodule-dir because git ignored them.
  3. Now, we have to commit in order to remove the files from the index:
    git commit
    After the commit, we cleaned the files that git followed and didn't followed in submodul-dir. Now it's time to do:
  4. git submodule add <remote-path-to-submodule>
    This will re-add the submodule but as a true submodule.
  5. At this point it might be a good idea to check .gitmodules and see if the submodules have been added successfully. In my case I already had an .gitmodules file so I had to modify it.

None of these solutions seemed to work for me so I figured my own:

  1. Make sure a new git repo already exist that will hold the content of the new submodule, for example, we'll be using "[email protected]:/newemptyrepo"

  2. Navigate to the directory you're modulizing:

cd myproject/submodule-dir
  1. Remove the to-be submodule from the parent's index:
git rm -r --cached .
  1. Init a new git repo inside the to-be submodule:
git init
  1. Set up the origin for the to-be submodule and make your first commit:
git remote add origin [email protected]:/newemptyrepo
git add . && git commit && git push --set-upstream origin master
  1. Now you must navigate to the parent repo's top-level path:
cd .. && cd `git rev-parse --show-toplevel`
  1. Finally, add the submodule as you would normally:
git submodule add [email protected]:/newemptyrepo ./myproject/submodule-dir
  1. Now commit & push the changes the above command makes and you're all set up!