简体   繁体   English

推送Git中的子模块更改

[英]Push submodule changes in Git

I have a Git submodule that I have successfully cloned in to my main project. 我有一个Git子模块,我已成功克隆到我的主项目中。 However, when I make changes in the submodule and try to push them back to the original submodule repository, Git refuses to do it. 但是,当我在子模块中进行更改并尝试将它们推回到原始子模块存储库时,Git拒绝执行此操作。 Why, and how can I fix this? 为什么,我该如何解决这个问题?

Example: I have project sub and project super in two separate folders. 示例:我在两个单独的文件夹中有project sub和project super。 I included sub as a submodule under super. 我把sub作为super下的子模块。 I make changes in folder super/sub, then try to push these changes to the original project sub. 我在文件夹super / sub中进行更改,然后尝试将这些更改推送到原始项目子。 Git complains. Git抱怨道。

EDIT: Regarding the error message, I'm using Git Bash on Windows and unfortunately cannot get the exact error message copied out of the terminal. 编辑:关于错误消息,我在Windows上使用Git Bash,遗憾的是无法从终端中获取确切的错误消息。 Here's the most pertinent lines: 这是最相关的部分:

remote: error: refusing to update checked out branch: refs/heads/master. remote:错误:拒绝更新签出的分支:refs / heads / master。 By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent with what you pushed, and will require 'git reset --hard' to match the work tree to HEAD. 默认情况下,拒绝更新非裸存储库中的当前分支,因为它会使索引和工作树与您推送的内容不一致,并且需要'git reset --hard'才能将工作树与HEAD匹配。

Note: Both repositories live on my hard drive. 注意:两个存储库都存在于我的硬盘上。

This has nothing to do with the repo being a submodule. 这与repo是一个子模块无关。

You are pushing to a git repo with a working copy (a non-bare repo), and are trying to push to the same branch that is currently checked out - by default git doesn't let you do that. 你正在推动一个带有工作副本(非裸仓库)的git仓库,并且正在尝试推送到当前签出的同一分支 - 默认情况下git不允许你这样做。

Before continuing you should ask yourself if you really want to do that. 在继续之前,你应该问问自己是否真的想这样做。 That's similar to, let's say you cloning a colleagues repo directly and then trying to push to it. 这类似于,让我们说你直接克隆同事回购然后试图推动它。 If you succeeded, it's possible that you could cause your colleague to loose work if they had uncommitted changes, or local files which when updated will be overwritten with tracked files. 如果您成功了,如果他们有未提交的更改,您可能会导致您的同事失去工作,或者更新后的本地文件将被跟踪的文件覆盖。

Easy solution 轻松解决方案

The easiest solution is to not push at all and just pull from your other repo instead. 最简单的解决方案是不要完全推动而只是从你的其他仓库拉出来。 The end result is the same, but since your're pulling updates in (and that's simply normal) git won't say anything about that. 最终的结果是一样的,但是由于你正在提取更新(而且这很正常),git不会对此有任何说明。

Slightly less-easy solution 稍微简单的解决方案

You can push into your other working copy, if you change the receiving repo's git config (read the rest of the error message) to allow it to be pushed into. 如果您更改接收repo的git配置(读取错误消息的其余部分)以允许将其推入,则可以推送到其他工作副本。 To do that, in the receiving repo: 为此,在收货回购中:

git config receive.denyCurrentBranch ignore

More details about that are in the help ( git config --help ) 关于它的更多细节在帮助中( git config --help

receive.denyCurrentBranch
    If set to true or "refuse", git-receive-pack will deny a ref update to the
    currently checked out branch of a non-bare repository. Such a push is potentially
    dangerous because it brings the HEAD out of sync with the index and working tree.
    If set to "warn", print a warning of such a push to stderr, but allow the push to
    proceed. If set to false or "ignore", allow such pushes with no message. Defaults
    to "refuse".

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM