Git submodule - How to keep the submodule updated when pulling from the main repo

喜夏-厌秋 提交于 2019-12-07 20:16:54

问题


We have the main git repository and one single git submodule. For this question, the main repository does only have a master branch, and the submodule does have two branches - X and Y.

Let's say Coder A just switched to submodule branch X and applied some small changes.
Coder B it's on submodule branch Y.

Now A pushes to the remote and B pulls - then it does not seem like B's active submodule commit (on their local machine) gets automatically changed to the commit that A pushed on branch X, even though A actively committed the changed submodule commit.
Instead for B, their version on branch Y stays active.

If B does now manually pull from the repository and set the branch to X, everything works again.

How can we achieve that submodule changes / active commits get automatically synced when pulling from the main repository?

We're using Source Tree as Git GUI if that makes any explanations easier.


回答1:


then it does not seam like B's active submodule commit (on their local machine) gets automatically changed to the commit that A pushed on branch X, even though A actively committed the changed submodule commit

You need to be sure A committed and push from the submodule, and then add/commit and push from the parent repo (in order to record the new submodule SHA1)

Since Git 1.7.5, git pull should update the submodules as well.
Double-check with a git submodule update --init --recursive

But don't forget that "updating a submodule" would only check out its SHA1, not a branch. Unless a submodule is set to track a branch, and you do a git submodule update --recursive --remote.
Even then, it would pull one branch (X or Y), not both.

A friend just told us that the repo on my friends machine might be broken. So we re-cloned it from Origin and now everything just works.

I'm now looking into tracking branches with submodules, to avoid the "checked out HEAD" issue. Can you recommend that?

As explained in the "True Nature of Submodules", a submodule is always in a detached HEAD mode at first.
You can still force it to follow a branch and update itself: see "git submodule tracking latest".
That means the following command would update the submodule content after pulling a branch said submodule must follow:

git submodule update --remote


来源:https://stackoverflow.com/questions/44916800/git-submodule-how-to-keep-the-submodule-updated-when-pulling-from-the-main-rep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!