gpt4 book ai didi

git - 为什么git子模块显示错误的分支

转载 作者:太空狗 更新时间:2023-10-29 13:46:54 26 4
gpt4 key购买 nike

我们有一个 super 项目和一个包含在其中的子模块。两者都有相同的分支 master、staging、featureBranch

我希望 master 上的 super 项目跟踪 master 上的子模块,staging 上的 super 项目跟踪 上的子模块分期

对于 super 项目中的 master,.gitmodules 看起来像这样:

[submodule "submodule"]
path = submodule
url = http://fpr-dev/submodule.git
branch = master

和分期

[submodule "submodule"]
path = submodule
url = http://fpr-dev/submodule.git
branch = staging

现在,当我在 master 上执行 git submodule 时,我看到以下输出:

$ git submodule
+e3782f37b1ae23aa0d5537ef3061dfdfec70f77f submodule (heads/Staging)

请注意它说的是暂存。但是当我在子模块中执行 git status 时,我看到:

$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

为什么会这样,我该如何解决?

现在,当我切换到 staging 并尝试 pull (git.exe pull -v --progress "origin") 子模块时,我收到以下错误:

You asked to pull from the remote 'origin', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

我想做的就是:

  • super 项目和子模块应该有相同的分支(Staging、feature、...)。
  • 当将 super 项目切换到不同的分支时,我希望子模块跟随 super 项目分支(即也切换到该分支)我不在乎这是自动完成还是我必须运行 1000 行脚本
  • 当在子模块中进行推或 pull 时,它应该可以正常工作(即推/pull 正确的分支)

实现此行为的最简单方法是什么?

最佳答案

当你运行 git submodule它为您的子模块显示了正确的提交 SHA,因为这是您在 super 项目中设置的。该命令不会告诉您当前本地子模块代码引用的提交 SHA;它告诉你想要 super 项目想要引用。

不幸的是,您必须采取额外的步骤来告诉您的 super 项目在本地提取特定的子模块代码。这是通过运行这些命令来完成的:

git submodule init

git submodule update

在此之后,您应该会在本地的子模块中看到正确的代码。您唯一可以绕过这些额外命令的时间是您第一次克隆存储库时。你可以这样称呼:

git clone <repo_name> --recursive

并且所有子模块都将在正确的提交 SHA 上 check out 。然而,一旦你 checkout 一个新分支,你就必须运行 initupdate再次命令。

要自动执行此行为,您可以创建一个脚本来运行而不是使用 git checkout直接命令。该脚本可以接受分支名称,并且看起来像这样:

git checkout <branch_name>

git submodule init

git submodule update

关于git - 为什么git子模块显示错误的分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com