远程存储库中的Git子模块

时间:2013-04-14 12:35:19

标签: git git-submodules git-bare

我无法理解子模块的工作原理。

我有一个远程存储库 PARENT 和一个远程存储库 CHILD 。我想将存储库 CHILD 设置为存储库 PARENT 的子模块,因此当我签出 PARENT 时, CHILD 将也可以签出(进入 PARENT 的子文件夹)。所以我的第一次尝试是将 PARENT 设置为裸存储库。但是当我想添加子模块时:

[git ~/repositories/PARENT] git submodule add git://github.com/CHILD.git CHILD
fatal: /usr/libexec/git-core/git-submodule cannot be used without a working tree.

所以我使用正确的工作树使 PARENT 成为非裸存储库(即使我真的不需要该存储库中的工作树)。添加子模块很顺利。将 PARENT 克隆到本地存储库也很顺利, CHILD 作为子文件夹被检出,正如预期的那样。然后,当我想将我的更改从本地存储库推回到 PARENT 时:

branch is currently checked out
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error: 
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error: 
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

所以我不能在我的裸存储库中使用子模块,因为它有一个工作树,但是当我使用非裸存储库时,我无法检入,因为它有一个工作树?我在这里不理解什么?

1 个答案:

答案 0 :(得分:2)

您的问题不在于子模块,而是由于尝试推送到非裸存储库(无论您是否正在推送包含submodule的提交)。

裸存储库是远程存储库(您不直接在存储库中工作)。要将子模块添加到远程存储库,请将子模块添加到本地存储库,然后git push将更改添加到远程存储库(从而将子模块作为提交推送到远程存储库中)。