git/submodule:更新后提前分离--变基

vhmi4jdf  于 2023-08-01  发布在  Git
关注(0)|答案(1)|浏览(122)

根据文档man git submodule update,如果使用--rebase选项运行,那么它永远不会使子模块的HEAD处于分离状态。

--rebase
           This option is only valid for the update command. Rebase the current branch onto the commit recorded in the superproject. If this option is given, the
           submodule’s HEAD will not be detached. If a merge failure prevents this process, you will have to resolve these failures with git-rebase(1). If the key
           submodule.$name.update is set to rebase, this option is implicit.

字符串
然而,在下面的一个基本示例中,它似乎没有按预期工作

$ pwd
/home/user/my_repo

$ cat .gitmodules 
[submodule "my_submodule"]
    path = my_submodule
    url = git@git.server:user/my_submodule.git
    branch = main

$ # This is a fresh clone, ensure that HEAD is detached
$ # This is an expected behavior so far

$ (cd my_submodule/ && git branch)
* (HEAD detached at f1129ce)
  main

$ # Let's update submodules with --rebase command
$ # The expected behavior is that HEAD will not
$ # be detached, but rather pointing to main

$ git submodule update --remote --init --rebase

$ # Nope :-( HEAD is still detached
$ (cd my_submodule/ && git branch)
* (HEAD detached at f1129ce)
  main

$ rpm -qf `which git`
git-core-2.39.1-1.fc37.x86_64
$ git --version
git version 2.39.1


我对文件的理解是错误的吗?有人能为我澄清一下吗?

u4dcyp6a

u4dcyp6a1#

将当前分支重定基到超级项目中记录的提交。如果给出了这个选项,子模块的HEAD将不会被分离。
但是在git submodule update --remote --init --rebase之前没有当前分支,HEAD已经分离。所以命令并没有神奇地将其移动到分支。您应该使用(cd my_submodule/ && git checkout main)将其移动到分支,然后使用rebase更新子模块。
请参阅https://stackoverflow.com/a/55570998/7976758了解更多信息。搜索https://stackoverflow.com/search?q=%5Bgit-submodules%5D+rebase+detached+HEAD

相关问题