0

When you locally create git submodules as on this script:

# Create the directory structure
mkdir main_repo
mkdir main_repo/unrelated_repo
mkdir main_repo/unrelated_repo/main_repo_submodule

cd main_repo

# Setup the unrelated_repo
cd unrelated_repo
printf "# UnRelated Repository\n\n" > README.md
git init
git add -f README.md
git commit -m "Added the unrelated repository first commit."
git remote add origin https://github.com/user/unrelated_repo

# Setup the main_repo_submodule
cd main_repo_submodule
printf "# Main Repository Submodule\n\nThis is a submodule from the \`main_repo\`, and not from the \`unrelated_repo\`\n" > README.md
git init
git add -f README.md
git commit -m "Added the main repository submodule first commit."
git remote add origin https://github.com/user/main_repo_submodule

# Setup the main_repo
cd ../..
printf "# Main Repo\n\nThis is the main repository which contains submodules\n" > README.md
printf "\nThis is a main_repo_file on the unrelated repository\n\n" > unrelated_repo/main_repo_file.txt
printf "\n*\n**\n\n" > unrelated_repo/.gitignore
git init
git add -f README.md unrelated_repo/main_repo_file.txt unrelated_repo/.gitignore
git submodule add -f -- https://github.com/user/main_repo_submodule "unrelated_repo/main_repo_submodule"

git commit -m "Added the first main repository first commit."
git remote add origin https://github.com/user/main_repo

The submodule .git folder will be located on the folder:

  1. main_repo/unrelated_repo/main_repo_submodule/.git/

However, if I push this submodule to a remote server do to perform a:

  1. git clone --recursive

The submodule git folder main_repo/unrelated_repo/main_repo_submodule/.git/ will be located on:

  1. main_repo/.git/modules/main_repo_submodule/

And on its place will be a synlink to its git folder on the parent repository. So how to put a submodule just create on this parent folder .git/modules/ folder, wihtout cloning it recursively?

It is necessary because I want to keep my submodules folder on the parent modules folder, keeping the workspace clean, without having to recursively re-clone everything every time a new submodule is added.

Update

I tried doing:

# Move the submodule to the parent repository
mkdir -p .git/modules
# shopt -s dotglob
mv unrelated_repo/main_repo_submodule/.git/ .git/modules
printf "gitdir: ../../.git/modules/main_repo_submodule\n" > unrelated_repo/main_repo_submodule/.git

But git says the submodule is not found.

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

1

After

mv unrelated_repo/main_repo_submodule/.git/ .git/modules

you also need

mv .git/modules/.git .git/modules/main_repo_submodule
phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    Thanks! I forgot the folder and looking on, I think it is better to just change the line send to the correct folder at once: `mv unrelated_repo/main_repo_submodule/.git/ .git/modules/main_repo_submodule`. – Evandro Coan Jun 20 '17 at 18:36