如何在分离HEAD状态下更新子模块至最新提交?

3

我试图更新我的子模块,使它们使用更近期的提交。

当我们使用子模块时,它们应该保持在一个分离的 HEAD 状态。这没问题。

$ cd myproject
$ cd otherlibrary
$ git status .
HEAD detached from 091eccc
nothing to commit, working tree clean

我曾经在otherlibrary上做了一些工作,现在我想更新myproject,以便它将使用otherlibrary中的这些新提交。这意味着我需要以某种方式“更新子模块”。 这样做是不起作用的:(看到了吗?它仍然具有相同的提交号)
$ cd myproject
$ cd otherlibrary
$ git submodule update --remote
$ git status .
HEAD detached from 091eccc
nothing to commit, working tree clean

为了完整性,我也尝试了这两种方式:git submodule update --rebase --remotegit submodule update --merge --remote,但是没有任何区别。
我还阅读了这两篇关于该主题的文章,但我的问题并没有被解决:

git submodule update --remote 有什么方面无法帮助的吗? - phd
忽略其他事实,只关心最终结果,结果是在执行该命令后,otherlibrary文件夹中的任何内容仍然不是最新的代码。 - 101010
1
git submodule update --remote must be run from the superproject: cd myproject but not cd otherlibrary - phd
好的,那个可以运行。但是如果我有10个子模块的情况下,我如何仅更新“otherlibrary”? - 101010
git submodule update --remote otherlibrary && git add otherlibrary && git commit -m "Update otherlibrary". Or cd otherlibrary && git checkout master && git pull && cd .. && git add otherlibrary && git commit -m "Update otherlibrary" - phd
1个回答

3

正如phd在评论中指出的,要使用git submodule update,您必须处于超级项目中。

(git submodule update --remote只是cd到子模块,运行git fetch,然后运行git checkout,因此如果由于某种原因您不想弹回超级项目,您可以自己运行git fetchgit checkout。 如果这样做,您可以浏览获取结果并仔细选择您想要的特定提交,而不仅仅是接受由origin/master或其他标识的提交。当然,如果您想要的就是由origin/master标识的提交,git submodule update --remote很方便。)


如果我有10个“其他项目”,而我不想将它们全部更新,该怎么办? - 101010
在这种情况下,您可以将 git submodule 命令限制为一个特定的子模块,或者——我更喜欢的方法——只需进入子模块并在那里工作,直到满意为止。 - torek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接