Search code examples
gitgit-submodules

How to switch git submodule with other branch properly?


I have repo, which contain lots of submodules. Every submodule was initialized with -b branch_rev_1. I want to switch state of this repo, changing every submodule's branch with -b branch_rev_2 (so I will be able to build app with different revision of dependencies).

But making checkout -b branch_rev_2 inside submodule's directory does not change record in .gitmodules file. Is there any way for switch state of all submodules after changing branch name in .gitmodules file (or vice versa, sync .gitmodules description and other configuration in .git folder after checking out specific revision inside submodule) ?


Solution

  • You can edit .gitmodules manually (using an editor) or using git config -f .gitmodules submodule.<name>.branch <branch_name>. After modifying sync it to .git/config with the command git submodule sync and update submodules using git submodule update --init --remote.