1 min read

Create a Git Submodule Inside an Existing Directory

How to create a git submodule inside an existing directory. In short, cheat and move the files in and out of the folder.
Create a Git Submodule Inside an Existing Directory

I have a repo that contains a media folder that is ignored by git. I wanted to add this folder, that already contains files, as a submodule to the existing repo.

Create an empty repo (I did this in Github and added a README.md) and then move into your main repo and clone the submodule into a temporary folder

git submodule add [email protected]:user/submodule.git submoduletmp

Edit .gitmodules and change the path to the correct folder

nano .gitmodule
[submodule "path/to/submodule"]
    path = path/to/submodule
    url = [email protected]:user/submodule
git submodule sync
git add .
git commit -m 'Initial commit of submodule'
git push origin master

Then move into the submodule folder and add and push the existing files here

cd path/to/submodule
nano .gitignore
git add .
git commit -m 'Initial commit of submodule'
git remote add origin [email protected]:user/submodule.git
git push -u origin master