【发布时间】:2017-05-04 00:38:01
【问题描述】:
我想要实现的是每当我在我的主存储库上使用 git checkout 时:
git checkout my-branch
我的子模块将跟随 my-branch 而不是分离的头。
有没有可能,如果有,怎么做?
【问题讨论】:
标签: git git-submodules bamboo
我想要实现的是每当我在我的主存储库上使用 git checkout 时:
git checkout my-branch
我的子模块将跟随 my-branch 而不是分离的头。
有没有可能,如果有,怎么做?
【问题讨论】:
标签: git git-submodules bamboo
如果这些子模块存储库有自己的 my-branch,它们可以是 declared to follow that branch
cd /path/to/your/parent/repo/Foo
git config -f .gitmodules submodule.bar1.branch my-branch
git config -f .gitmodules submodule.bar2.branch my-branch
git submodule update --remote
但这涉及到每次您在父 repo 中签出一个分支时都重复这一点。
torek 指出in the comments 这些子模块可能有自己的子模块,因此需要--recursive 选项。
您可能还想将
--recursive和/或--no-fetch添加到您的git submodule update --remote命令中。
您可能需要git submodule foreach,而不是单独的git config -f操作,也可能需要--recursive。
git submodule foreach -q --recursive 'git config -f $toplevel/.gitmodules submodule.$name.branch my_branch'
为了便于阅读,多行:
git submodule foreach -q --recursive \
'git config -f $toplevel/.gitmodules submodule.$name.branch my_branch'
然后您可以将每个子模块签出到该分支:
git submodule foreach -q --recursive 'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; git checkout $branch'
为了便于阅读,多行:
git submodule foreach -q --recursive \
'branch="$(git config -f $toplevel/.gitmodules submodule.$name.branch)"; \
git checkout $branch'
【讨论】:
--recursive 和/或--no-fetch 添加到您的git submodule update --remote 命令中。您可能需要git submodule foreach,而不是单独的git config -f 操作,也可能需要--recursive,尽管要做到这一点看起来很棘手。