Git推送子模块

3

我使用git clone . /somedirectory/b.git将仓库A克隆到仓库B。后来,我决定在B中添加子模块C git submodule add /path/to/c.git c_in_b。那么我该如何将这个子模块的添加推送回A?

尝试使用git push,但出现以下错误。

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

1
你不能像那样推送到一个“非裸”的存储库。https://dev59.com/0nA75IYBdhLWcg3wkZ-A - Dogbert
3个回答

3

这是因为您正在尝试向非裸仓库推送。首先创建裸仓库以供推送。只需在克隆命令中添加--bare选项即可。

发生这种情况的原因是为了防止工作丢失。如果分支将在外部更新,则有工作目录意味着您可能会丢失工作。


2

就像其他答案所指出的那样,似乎有些人混淆了存储库作为工作副本和存储库作为裸存储库(~服务器)之间的区别。

最好的方法是在心理上区分这两者。工作副本是您实施更改、提交并将其推送到“服务器存储库”的地方。服务器存储库通常是裸存储库,正是因为您在尝试推送时可以看到的原因。工作副本可能包含会被修改工作副本中的底层.git目录覆盖的更改。

最简单的解决方法可能是将远程存储库指向“服务器存储库”,即保留更改的裸存储库。如果您还没有服务器存储库,您可能希望为模块创建它们。之后,您可以执行以下操作:

git remote rm origin
git remote add origin ssh://username@remote_repository

对于子模块和顶级仓库,都需要进行操作。

1

这是git的一般行为,即使没有子模块也是如此。您正在向一个“工作库”或“非裸库”(A)推送,而git默认情况下不允许您这样做,您看到的错误消息显示了原因和解决方法。

在您的情况下,我认为您可以更新A中的配置(将receive.denyCurrentBranch设置为warnignore),如错误提示所示,或者只需从A上检出另一个分支,然后从B上推送。长期使用时,请使用裸库进行推送。


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