我有一个包含以下.gitmodules
文件的父项目
[submodule "src/redux"]
path = src/redux
url = git@github.com:username/sub-package.git
branch = master
以下是我用来克隆父项目的步骤,以确保我设置了子模块。
git clone git@github.com:username/parent-project.git
git submodule init
git submodule update
之后,我在父项目中的地方有子模块的内容。但是,HEAD在子模块中分离。首先表明我没有做对。我希望这是在主分支上,因为父项目的.gitmodules
文件指向主分支。
然后我检查了master并设置了子模块的master分支来跟踪远程主分支。
git checkout master
git branch -u origin/master master
当我退回到父项目并运行git status时,我在子模块中有一个未提交的更改。
当我刚开始设置它时,我觉得我不应该有这种改变。如果我这样做,那么克隆项目的所有开发人员在设置本地环境时也会有自己独特的初始提交。我究竟做错了什么?
答案 0 :(得分:0)
子模块使用起来有点棘手;如果你有它们,你需要确保团队中的每个开发人员都能理解Git模型和它如何处理子模块。
将子模块添加到repo时,子模块的内容永远不会添加到repo本身;父repo记录的关于子模块的 only 事物是它对应的提交哈希。如果您运行git diff -- src/redux
,则可以看到此内容。
这意味着当您git submodule init
时,您正在更新子模块的工作树以反映存储在索引中的哈希值;来自git-submodule(1)
:
init
Initialize the submodules recorded in the index (which were added
and committed elsewhere) by copying submodule names and urls from
.gitmodules to .git/config. Optional <path> arguments limit which
submodules will be initialized. It will also copy the value of
submodule.$name.update into .git/config. The key used in
.git/config is submodule.$name.url. This command does not alter
existing information in .git/config. You can then customize the
submodule clone URLs in .git/config for your local setup and
proceed to git submodule update; you can also just use git
submodule update --init without the explicit init step if you do
not intend to customize any submodule locations.
.gitmodules
存储有关子模块配置的其他信息,特别是更新子模块时使用submodule.src/redux.branch
,即将其指向更近的提交;也来自git-submodule(1)
:
--remote
This option is only valid for the update command. Instead of using
the superproject’s recorded SHA-1 to update the submodule, use the
status of the submodule’s remote-tracking branch. The remote used
is branch’s remote (branch.<name>.remote), defaulting to origin.
The remote branch used defaults to master, but the branch name may
be overridden by setting the submodule.<name>.branch option in
either .gitmodules or .git/config (with .git/config taking
precedence).