将公共代码添加为 git 子模块的问题:“已存在于索引中”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12898278/
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-10 14:46:36  来源:igfitidea点击:

Issue with adding common code as git submodule: "already exists in the index"

gitgit-submodules

提问by Vanja

I'm new to git and would appreciate help with adding submodules. I've received two projects sharing some common code. The shared code was just copied into the two projects. I created a separate git repo for the common code and removed it from the projects with the plan to add it as a git submodule.

我是 git 的新手,希望在添加子模块方面得到帮助。我收到了两个共享一些通用代码的项目。共享代码刚刚复制到两个项目中。我为公共代码创建了一个单独的 git repo,并将其从项目中删除,并计划将其添加为 git 子模块。

I used the path option of git submodule add to specify the folder:

我使用 git submodule add 的 path 选项来指定文件夹:

git submodule add url_to_repo projectfolder

but then got the error:

但随后出现错误:

'projectfolder' already exists in the index"

This is the desired structure of my repository:

这是我的存储库所需的结构:

repo
|-- projectfolder
    |-- folder with common code

It is possible to add the git submodule directly in the repo or into a new folder there, but not in the projectfolder. The problem is that it really need to be in the projectfolder.. What can I do about this and what have I misunderstood about the path option of git submodule add?

可以将 git 子模块直接添加到 repo 中或添加到那里的新文件夹中,但不能添加到项目文件夹中。问题是它真的需要在项目文件夹中..我该怎么办,我对 git submodule add 的路径选项有什么误解?

回答by Mark Longair

I'm afraid there's not enough information in your question to be certain about what's going on, since you haven't replied to my follow-up question, but this may be of help in any case.

恐怕您的问题中没有足够的信息来确定发生了什么,因为您还没有回答我的后续问题,但这在任何情况下都可能有所帮助。

That error means that projectfolderis already staged ("already exists in the index"). To find out what's going on here, try to list everything in the index under that folder with:

该错误意味着projectfolder已经上演(“已经存在于索引中”)。要了解这里发生了什么,请尝试列出该文件夹下索引中的所有内容:

git ls-files --stage projectfolder

The first column of that output will tell you what type of object is in the index at projectfolder. (These look like Unix filemodes, but have special meanings in git.)

该输出的第一列将告诉您索引中的对象类型projectfolder。(这些看起来像 Unix 文件模式,但在 git 中有特殊含义。)

I suspect that you will see something like:

我怀疑你会看到类似的东西:

160000 d00cf29f23627fc54eb992dde6a79112677cd86c 0   projectfolder

(i.e. a line beginning with 160000), in which case the repository in projectfolderhas already been added as a "gitlink". If it doesn't appear in the output of git submodule, and you want to re-add it as a submodule, you can do:

(即以 开头的行160000),在这种情况下,存储库中的存储库projectfolder已经添加为“gitlink”。如果它没有出现在 的输出中git submodule,并且您想将其重新添加为子模块,则可以执行以下操作:

git rm --cached projectfolder

... to unstage it, and then:

...取消暂存,然后:

git submodule add url_to_repo projectfolder

... to add the repository as a submodule.

... 将存储库添加为子模块。

However, it's also possible that you will see many blobs listed (with file modes 100644and 100755), which would suggest to me that you didn't properly unstage the files in projectfolderbefore copying the new repository into place. If that's the case, you can do the following to unstage all of those files:

但是,您也可能会看到列出了许多 blob(带有文件模式100644100755),这表明您projectfolder在将新存储库复制到位之前没有正确地取消暂存文件。如果是这种情况,您可以执行以下操作来取消所有这些文件的暂存:

git rm -r --cached projectfolder

... and then add the submodule with:

...然后添加子模块:

git submodule add url_to_repo projectfolder

回答by Gajen Sunthara

Removing submodule manually involves number of steps and this worked for me.

手动删除子模块涉及许多步骤,这对我有用。

Assuming you are in the project root directory and sample git module name is "c3-pro-ios-framework"

假设您在项目根目录中并且示例 git 模块名称是“c3-pro-ios-framework”

Remove the files associated to the submodule

删除与子模块关联的文件

rm -rf .git/modules/c3-pro-ios-framework/

Remove any references to submodule in config

在配置中删除对子模块的任何引用

vim .git/config

enter image description here

在此处输入图片说明

Remove .gitmodules

删除 .gitmodules

rm -rf .gitmodules

Remove it from the cache without the "git"

将它从缓存中删除,没有“git”

git rm --cached c3-pro-ios-framework

Add submodule

添加子模块

git submodule add https://github.com/chb/c3-pro-ios-framework.git

回答by Neeraj Kumar

You need to remove your submodule git repository (projectfolder in this case) first for git path.

您需要首先删除 git 路径的子模块 git 存储库(在本例中为项目文件夹)。

rm -rf projectfolder

git rm -r projectfolder

and then add submodule

然后添加子模块

git submodule add <git_submodule_repository> projectfolder

回答by Shelton

I had the same problem and after hours of looking found the answer.

我遇到了同样的问题,经过数小时的查找找到了答案。

The error I was getting was a little different: <path> already exists and is not a valid git repo(and added here for SEO value)

我得到的错误有点不同:(<path> already exists and is not a valid git repo并在此处添加以获取 SEO 价值)

The solution is to NOT create the directory that will house the submodule. The directory will be created as part of the git submodule addcommand.

解决方案是不创建将容纳子模块的目录。该目录将作为git submodule add命令的一部分创建。

Also, the argument is expected to be relative to the parent-repo root, not your working directory, so watch out for that.

此外,该参数预计与父存储库根目录相关,而不是您的工作目录,因此请注意这一点。

Solution for the example above:

上面例子的解决方案:

  1. It IS okay to have your parent repo already cloned.
  2. Make sure the common_codedirectory does notexist.
  3. cd Repo
  4. git submodule add git://url_to_repo projectfolder/common_code/(Note the required trailing slash.)
  5. Sanity restored.
  1. 可以克隆您的父存储库。
  2. 确保common_code目录也不会存在。
  3. cd Repo
  4. git submodule add git://url_to_repo projectfolder/common_code/注意所需的尾部斜杠。
  5. 恢复了理智。

I hope this helps someone, as there is very little information to be found elsewhere about this.

我希望这对某人有所帮助,因为在其他地方找不到关于此的信息。

回答by justbilt

if there exists a folder named xunder git control, you want add a same name submodule , you should delete folder xand commit it first.

如果存在xgit控制下的文件夹,你想添加一个同名的子模块,你应该删除文件夹x并先提交它

Updated by @ujjwal-singh:

由@ujjwal-singh 更新:

Committing is not needed, staging suffices.. git add/ git rm -r

承诺是不需要的,分期就足够了.. git add/git rm -r

回答by Dylan Pierce

Just to clarify in human language what the error is trying to tell you:

只是为了用人类语言澄清错误试图告诉你什么:

You cannot create a repository in this folder which already tracked in the main repository.

您不能在此文件夹中创建已在主存储库中跟踪的存储库。

For example: You have a theme folder called AwesomeThemethat's a dedicated repository, you try do dump it directly into your main repository like git submodule add sites/themesand you get this "AwesomeTheme" index already exists.

例如:您有一个名为的主题文件夹AwesomeTheme,这是一个专用存储库,您尝试将其直接转储到您的主存储库中,git submodule add sites/themes然后您会得到这个"AwesomeTheme" index already exists.

You just need to make sure there isn't already a sites/themes/AwesomeThemein the main repository's version tracking so the submodule can be created there.

您只需要确保sites/themes/AwesomeTheme主存储库的版本跟踪中还没有一个,以便可以在那里创建子模块。

So to fix, in your main repository if you have an empty sites/theme/AwesomeThemedirectory then remove it. If you have already made commits with the sites/theme/AwesomeThemedirectory in your main repository you need to purge all history of it with a command like this:

因此,要修复,在您的主存储库中,如果您有一个空sites/theme/AwesomeTheme目录,则将其删除。如果您已经对sites/theme/AwesomeTheme主存储库中的目录进行了提交,则需要使用如下命令清除它的所有历史记录:

git filter-branch --index-filter \
              'git rm -r --cached --ignore-unmatch sites/theme/AwesomeTheme'     HEAD

Now you can run git submodule add [email protected] sites/themes/AwesomeTheme

现在你可以运行 git submodule add [email protected] sites/themes/AwesomeTheme

Since the main repository has never seen anything (a.k.a index'd) in sites/themes/AwesomeThemebefore, it can now create it.

由于主存储库以前从未见过任何东西(又名索引)sites/themes/AwesomeTheme,它现在可以创建它。

回答by Vanja

I got it working by doing it the other way around. Starting with an empty repo, adding the submodule in a new folder called "projectfolder/common_code". After that it was possible to add the project code in projectfolder. The details are shown below.

我通过相反的方式让它工作。从一个空的 repo 开始,将子模块添加到一个名为“projectfolder/common_code”的新文件夹中。之后就可以在项目文件夹中添加项目代码了。详情如下所示。

In an empty repo type:

在空的回购类型中:

git submodule add url_to_repo projectfolder/common_code

That will create the desired folder structure:

这将创建所需的文件夹结构:

repo
|-- projectfolder
    |-- common_code

It is now possible to add more submodules, and the project code can be added to projectfolder.

现在可以添加更多子模块,并且可以将项目代码添加到项目文件夹中。

I can't yet say why it worked this way around and not the other.

我还不能说为什么它以这种方式而不是另一种方式起作用。

回答by Lawrence

Don't know if this is any useful, though I had the same problem when trying to commit my files from within IntelliJ 15. In the end, I opened SourceTree and in there I could simply commit the file. Problem solved. No need to issue any fancy git commands. Just mentioning it in case anyone has the same issue.

不知道这是否有用,尽管我在尝试从 IntelliJ 15 中提交文件时遇到了同样的问题。最后,我打开了 SourceTree,在那里我可以简单地提交文件。问题解决了。无需发出任何花哨的 git 命令。只是提到它以防万一有人有同样的问题。

回答by Sam Biswas

Go to the repository folder. Delete relevant submodules from .gitmodules. Select show hidden files. Go to .git folder, delete the submodules from module folder and config.

转到存储库文件夹。从 .gitmodules 中删除相关的子模块。选择显示隐藏文件。转到 .git 文件夹,从模块文件夹和配置中删除子模块。

回答by seektrue

In your git dir, suppose you have sync all changes.

在您的 git 目录中,假设您已同步所有更改。

rm -rf .git 

rm -rf .gitmodules

Then do:

然后做:

git init
git submodule add url_to_repo projectfolder