我正在使用 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/