Git 子模块 - 从主存储库中提取时如何保持子模块更新 [英] Git submodule - How to keep the submodule updated when pulling from the main repo

查看:74
本文介绍了Git 子模块 - 从主存储库中提取时如何保持子模块更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有主 git 存储库和一个单独的 git 子模块.对于这个问题,主存储库只有一个主分支,子模块有两个分支 - XY.

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.

假设 Coder A 刚刚切换到子模块分支 X 并应用了一些小的更改.
编码器 B 它在子模块分支 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.

现在 A 推送到远程,B 拉取 - 那么它看起来不像 B 的活动子模块提交(在他们的本地machine) 会自动更改为 A 在分支 X 上推送的提交,即使 A 主动提交了更改的子模块提交.
对于 B,他们在分支 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.

如果 B 现在从存储库中手动拉取并将分支设置为 X,则一切都会再次运行.

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?

我们使用 Source Tree 作为 Git GUI,如果这可以使任何解释更容易.

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

推荐答案

那么它不会像 B 的活动子模块提交(在他们的本地机器上)自动更改为 A 在分支 X 上推送的提交,即使 A 主动提交了更改的子模块提交

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

您需要确保 A 提交并从子模块推送,然后从父存储库添加/提交和推送(为了记录新的子模块 SHA1)

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)

自 Git 1.7.5 起,git pull 也应更新子模块.
仔细检查 git submodule update --init --recursive

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

但是不要忘记更新子模块"只会检查其 SHA1,而不是分支.除非 子模块设置为跟踪分支,并且您执行 git submodule update --recursive --remote.
即便如此,它也会拉一个分支(X或Y),而不是两者.

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.

一位朋友刚刚告诉我们,我朋友机器上的存储库可能已损坏.所以我们从 Origin 重新克隆了它,现在一切正常.

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.

我现在正在研究带有子模块的跟踪分支,以避免检出 HEAD"问题.你能推荐一下吗?

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

子模块的真实性质"中所述,子模块始终处于分离的 HEAD 模式第一.
您仍然可以强制它跟随一个分支并自行更新:请参阅git submodule tracking latest".
这意味着以下命令将在拉取一个子模块必须遵循的分支后更新子模块内容:

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

这篇关于Git 子模块 - 从主存储库中提取时如何保持子模块更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆