Search code examples
gitgitlabgit-submodulescicd

git submodule update --init doesn't fetch all branches from remote submodule repository


I have a parent repository A with a submodule repository B within it. I am running a CI/CD pipeline in gitlab in the repository A

Test_submodule:
  stage: tests
  image:
    name: .....
    entrypoint: [""]
  script:
    - git submodule update --init
    - cd submodule_dir
    - git branch -a
    - git checkout master
    - git pull

I want to checkout to the master branch and use code from here however its not returning the master branch. Only the default branch and the submodule commit are available

Already verified that the repository B has master branch.

Also tried

variables:
  GIT_SUBMODULE_STRATEGY: normal

Gitlab version - 16.11


Solution

  • The command git submodule update --init only initialises the submodule using the .gitmodules configuration and the default commit.

    The .gitmodules configuration can contain something like this:

    [submodule "submoduleDir"]
        path = submoduleDir
        url = https://submodule.com
        branch = develop
    

    If you want the submodule to point to a specific branch other than the default one, like master, you don't need to go into submodule directory, but run the command git submodule set-branch -b {MyBranchName} ./submoduleDir. The command will set the branch for the submodule only.

    It won't checkout yet the branch you selected, unless you run the command git -c submodule update --remote to update the submodule using the branch you specified above.

    Also, what I usually do is running git -c submodule sync to synchronise the submodule's branches before update --remote command.