Search code examples
gitgithubversion-controlgit-submodules

Creating multiple remote branches with git submodules


I am working with git submodules for the first time. Struggling with understanding how to create branches across the board and adding them to all the remote repos.

Currently my file structure resembles the following:

-parent_repo
  |
  |_ submodule_1
  |_ submodule_2
  |_ submodule_3
  |_ submodule_4
  |_ submodule_5
  |_ submodule_6
  |_ submodule_7

If I create a branch on parent repo:

(master) $ git checkout -b feature/my_feature
(feature/my_feature) $ git commit -m "created my_feature"
(feature/my_feature) $ git push -u origin feature/my_feature

I would like to create a branch across all submodules including the parent. After which all branches are pushed remote to each submodules and their respected repos.

Tried the following steps:

$ git submodule foreach -b branch_name
$ git push --recurse-submodules=on-demand
$ git submodule foreach "(git checkout branch_name; git pull)&"

..just fails. First command not found.

..and if I do:

$ git config -f .gitmodules submodule.submodule_1.branch branch_name
$ git submodule update --remote

git returns:

fatal: Needed a single revision
Unable to find current origin/branch_name revision in submodule path 'submodule_1'

Solution

  • See Submodule tips:

    For example, let’s say we want to start a new feature or do a bugfix and we have work going on in several submodules.
    We can easily stash all the work in all our submodules:

    $ git submodule foreach 'git stash'
    Entering 'CryptoLibrary'
    No local changes to save
    Entering 'DbConnector'
    Saved working directory and index state WIP on stable: 82d2ad3 Merge from origin/stable
    HEAD is now at 82d2ad3 Merge from origin/stable
    

    Then we can create a new branch and switch to it in all our submodules:

    $ git submodule foreach 'git checkout -b featureA'
    Entering 'CryptoLibrary'
    Switched to a new branch 'featureA'
    Entering 'DbConnector'
    Switched to a new branch 'featureA'
    

    Then you still need to create the same branch in the parent repo, add, commit and push, as all submodule repos will have changed.

    But remember: those are different branches (even if they have the same name), each one specific to its own repo (either the parent repo, or a submodule repo).