gitbranchgit-submodulesfatal-error

Git switch to branch including submodules return fatal error


I am facing an issue using git submodules. My git version is 2.25.1 (on Ubuntu 20.04) and its structure is as following:

I am at an early state of the project and needed an external library that I included as a submodule. Since I am developing, the submodule is only on branch "A".

When I clone the repository using:

git clone --recurse-submodules <my_git_repository>

Then switch to branch "A", I get the following fatal error: fatal: not a git repository: <relative_path_to_the_external_library>

I am finally stuck outside the branch "A" with added material on the branch where I am.

I found two workarounds:

  1. clone my git repository from its branch "A" directly then switch to the branch "A"
  2. clone my git repository from its main branch, run: git pull origin <branch "A"> and then switch to the branch "A"

Is it the normal way to include submodules inside a branch and use them? I was hoping to be able to switch directly to the branch... The thing is that when I clone my git repo it does not clone the submodule since it is not on the main branch. Then when I would like to switch it does not clone it either so the path to the submodule is not a git repo because it is empty / not found.

Thank you for your help!


Solution

  • Today it is working well. It seems that the git pull origin <branch "A"> did update remote configuration too. Now the git repository is good and I can switch to branch "A" then initialize the submodule for a fresh clone.