【问题标题】:Is it possible to checkout branches of main repository in sync with submodules?是否可以与子模块同步检出主存储库的分支?
【发布时间】:2017-05-04 00:38:01
【问题描述】:

我想要实现的是每当我在我的主存储库上使用 git checkout 时:

git checkout my-branch

我的子模块将跟随 my-branch 而不是分离的头。

有没有可能,如果有,怎么做?

【问题讨论】:

    标签: git git-submodules bamboo


    【解决方案1】:

    如果这些子模块存储库有自己的 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,尽管要做到这一点看起来很棘手。
    • 我同意并且稍后会编辑(我正在通勤)我只是提出了一个解决方案的开始,提到了为子模块跟踪分支的可能性。
    • 感谢您的回复。感谢您的帮助,但是我创建了一个脚本,子模块将遵循主存储库分支。
    • @GilroyToledano Nice:您的脚本在某处的要点中可用吗? (gist.github.com)
    猜你喜欢
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2017-11-19
    • 1970-01-01
    • 2020-12-11
    • 2017-01-15
    相关资源
    最近更新 更多