git - 更改 git 模块分支

标签 git git-branch git-submodules

我正在使用 git submodule 来跟踪子项目。子模块指向特定分支,但我现在需要更改它。

我已经尝试手动编辑 .gitmodules 文件并更改 branch = 条目,但是我稍后提供的每个更新 repo 的命令都没有做任何事情或失败。

我试过了

git submodule sync # Succeeds, but nothing happens
git submodule update # Doesn't do anything
git submodule update --remote # fails with Needed a single revision

子项目没有正确 check out ,那里的 gitk 显示它确实有我需要切换到的新分支。我可以手动执行此操作并提交新哈希,但我不确定 git submodule 是否会真正理解这种方式的更改。

我还尝试删除所有子模块文件夹(包括 .git 文件夹内)并重新运行

git submodule update --init --recursive --remote

但我仍然将项目恢复到与以前相同的状态(我假设是因为它仍在更新到已提交的哈希值)。

最佳答案

再次克隆,然后:

  • 在子模块中更改分支

       cd submodule
       git checkout -b anotherBranch
       # if it is a new branch, push it first.
       git push -u origin anotherBranch
    

然后在父仓库中反射(reflect)这个变化:

    cd ..

    git config -f .gitmodules submodule.<pathToSubmodule>.branch anotherbranch
    # or, since Git 2.22 (Q2 2019)
    git submodule set-branch --branch anotherbranch -- path/To/Submodule

    git add .
    git commit -m "record new gilink, and modified .gitmodules"
    git push

然后尝试再次克隆您的父存储库以检查一切是否符合预期。

(同样,Git 2.22,2019 年第二季度,有 introduced git submodule set-branch --branch aBranch -- <submodule_path> )

关于git - 更改 git 模块分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48449397/

相关文章:

git - 在 git 裸仓库中切换当前分支

git - 如何创建远程 Git 分支?

Windows 上 Jenkins 中的 git 插件不使用子模块的凭据

git - 除了子模块中的新提交之外,我如何 `git add -u`?

git clone 不 checkout 事件分支

Git 使用现有的 Git 存储库添加文件夹

git - 如何将 Git 子模块指针恢复为存储在包含存储库中的提交?

git - 将文件与另一个提交中的另一个文件进行比较

Git 作者在他们的名字后面加上一个星号

git - 如何在 NFS 分区中拥有 git 存储库,在本地分区中拥有工作树?