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

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

问题描述

我们有主git仓库和一个git子模块。对于这个问题,主存储库只有一个主分支,子模块确实有两个分支 - X Y



假设Coder A 刚切换到子模块分支 X 并应用了一些小的更改。

Coder B 它在子模块分支 Y 上。


$ b 现在 A 推送到远程, B 拉 - 然后它看起来不像 B 的活动子模块提交(在其本地计算机上)会自动更改为 A 在分支 X 上推送的提交,即使 A 主动提交已更改的子模块提交。

对于 B ,其分支 Y 上的版本保持活动状态。


如果 B 现在从存储库手动提取并将分支设置为 X ,则所有操作都会再次执行。 / p>

我们如何实现从主存储库中提取的子模块更改/活动提交会自动同步?



我们'使用Source Tree作为Git GUI,如果这使得任何解释更容易。

解决方案


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


您需要确保A已提交并从子模块中进行推送,然后然后添加/提交并从父回购库中推入(以便记录新的子模块SHA1) / p>

自从Git 1.7.5 git pull 应该更新子模块。

仔细检查 git子模块update --init --recursive



但请不要忘记,更新子模块只会检出其SHA1,而不是分支。除非子模块设置为跟踪分支,并且您执行 git子模块更新 - 递归--remote

即使这样,它也会将一个分支(X或Y)拉出来,而不是两者。


一位朋友告诉我们,我朋友机器上的回购可能被破坏。所以我们从Origin重新克隆它,现在一切正常。

我现在正在研究跟踪具有子模块的分支,以避免签出HEAD问题。你能推荐吗?

正如真实性质的子模块,子模块首先总是处于分离的HEAD模式。

您仍可以强制它遵循分支并自行更新:请参阅 git submodule tracking latest

这意味着以下命令会在拉出分支后更新子模块内容,所述子模块必须遵循: p>

  git submodule update --remote 


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.

解决方案

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

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

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