We have a repo that has a few submodules. One of the submodule directories is an API. Depending on the branch of the main repo, the API subdirectory either points to API_V1 repo or API_V2 repo. If I clone the main repo, and am on develop, it points to API_V2 when I submodule init/update. But if I switch to an very old branch instead of develop, and do a submodule update or init/update git is unable to find the correct commit in the API submodule because it's still using API_V2. Is it possible to get git to change to the correct repo in the API submodule. Our devops did this to avoid having to rename the API subdir to API_V1 and API_V2, which would have needed many script changes. Thanks
After switching branches with git checkout
or git switch
in the superproject (parent repository) an old .gitmodules
is checked out. If the file has different URLs for the submodules the URLs should be updated in .git/config
because Git doesn't use URLs from .gitmodules
directly, it only uses .git/config
. The command to update the URLs is git submdule sync
(add --recursive
if you have a tree of submodules). After that git submodule update --init
should update submodules to the respective commits stored in the superproject.
You can automate the process with post-checkout
hook. Put this
#!/bin/sh
new_branch="$3"
if [ "$new_branch" = 1 ]; then
git submodule sync --recursive
git submodule update --init --recursive
fi
exit 0
in .git/hooks/post-checkout
, make the file executable by running chmod a+x .git/hooks/post-checkout