无法将 git 子模块添加到存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10418695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:52:05  来源:igfitidea点击:

can't add git submodule to repository

gitgithubgit-submodulespyrocms

提问by William Turrell

(Predominantly a Git question, but may be relevant to other PyroCMS users)

(主要是 Git 问题,但可能与其他 PyroCMS 用户相关)

I have a local repository of PyroCMS - the repo is a clone of a github fork of the main project. I also have a PyroCMS module, which is also a local clone of a github fork of thatproject.

我有一个 PyroCMS 的本地存储库 - 该存储库是主项目的 github 分支的克隆。我还有一个 PyroCMS 模块,它也是项目的 github 分支的本地克隆。

I've put them both in separate directories.

我把它们放在不同的目录中。

~/Dropbox/websites/pyrocmscommunity-test/
~/Dropbox/github/PyroDatabase/

I want to add PyroDatabase as a submoduleof pyrocmscommunity-test, so I can pull updates from github, keep track of my own changes etc.

我想将 PyroDatabase 添加为pyrocmscommunity-test的子模块,以便我可以从 github 中提取更新,跟踪我自己的更改等。

I tried to do this by going to the top of the working tree and doing:

我试图通过转到工作树的顶部并执行以下操作来做到这一点:

git submodule add ../../github/PyroDatabase/ addons/shared_addons/modules/

but it didn't complete properly:

但它没有正确完成:

Cloning into 'addons/shared_addons/modules/database'...
ssh_exchange_identification: Connection closed by remote host
fatal: The remote end hung up unexpectedly

I don't understand this as I didn't specify an SSH connection, I just wanted to use the local repo. What is it trying to connect to and why?

我不明白这一点,因为我没有指定 SSH 连接,我只想使用本地存储库。它试图连接什么,为什么?

Also, now, whenever I repeat the command, I get this:

另外,现在,每当我重复命令时,我都会得到以下信息:

'addons/shared_addons/modules' already exists in the index

But I don't understand this as there is no .gitmodulesfile and no mention of the modules files in .gitconfigeither.

但我不明白这一点,因为没有.gitmodules文件,也没有提到模块文件.gitconfig

What am I doing wrong and how do I reset things?

我做错了什么,我该如何重置?

Thanks, William

谢谢,威廉

回答by CharlesB

When specifying relative URLs for submodule location, Git takes it relatively to the origin(here Github) URL of the current projet, and not relatively to the project location on filesystem. This is because a submodule is a permanent pointer to a remote location. Here Git tries to contact the origin github repository with two levels up, which is an incorrect URL.

当为子模块位置指定相对 URL 时,Git 将它相对于当前项目的origin(这里是 Github)URL,而不是相对于文件系统上的项目位置。这是因为子模块是指向远程位置的永久指针。这里Git尝试联系上两层的原始github仓库,这是一个错误的URL。

As for the second error Git has created an entry in the staging area of your index, and it must be removed (unstaged) with

至于第二个错误,Git 已经在你索引的暂存区创建了一个条目,它必须被删除(取消暂存)

git rm -r addons/shared_addons/modules

Then retry with the absolute path to your clone:

然后使用克隆的绝对路径重试:

git submodule add $HOME/Dropbox/github/PyroDatabase/ addons/shared_addons/modules/

Anyway adding a local clone of your project as a submodule of itself is weird, since submodules are meant to be a different repo of the containing project. Why not keep track of local and origin commits in standard Git fashion?

无论如何,将项目的本地克隆添加为自身的子模块是很奇怪的,因为子模块是包含项目的不同存储库。为什么不以标准 Git 方式跟踪本地和源提交?

回答by djs

What version of git are you running? You probably cannot run the command again because the directory structure you specified has been staged. You can reset things like this (be careful with this command):

你运行的是什么版本的git?您可能无法再次运行该命令,因为您指定的目录结构已暂存。你可以像这样重置(小心这个命令):

$ git status # check what has been changed
$ git reset --hard HEAD # destroy all working copy changes

A really useful way to debug what's going on with git is to set GIT_TRACE=1. So, try running the command again once you've cleaned up like this:

调试 git 正在发生的事情的一个非常有用的方法是设置GIT_TRACE=1. 因此,像这样清理完后,再次尝试运行命令:

$ GIT_TRACE=1 git submodule add ../../github/PyroDatabase/ addons/shared_addons/modules/