无法检出 git 子模块路径

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

Unable to checkout git submodule path

gitgit-submodules

提问by Richard Tuin

I have a problem when working with git submodules.

使用 git 子模块时遇到问题。

Whenever i receive a new submodule reference from the upstream repository, executing git submodule updategives the following result:

每当我从上游存储库收到新的子模块引用时,执行git submodule update都会给出以下结果:

fatal: reference is not a tree: dd208d46ecdd1ac0d2b2594a610fe4c9150fece1
Unable to checkout 'dd208d46ecdd1ac0d2b2594a610fe4c9150fece1' in submodule path 'submodule/path'

It is important to note that the submodule has several remotes, of which the upstream remote should be used to update the submodule reference tree. I'm guessing that my problem is there, but i am not sure.

需要注意的是,子模块有几个遥控器,其中上游遥控器应该用于更新子模块引用树。我猜我的问题就在那里,但我不确定。

My setup is the following:

我的设置如下:

Git project

Git项目

Remotes:

遥控器:

  1. origin(my git fork)
  2. upstream(project repo)
  1. origin(我的 git 叉子)
  2. upstream(项目回购)

Submodule "module", has remotes:

子模块“模块”,有遥控器:

  1. origin(my git fork)
  2. upstream(project repo)
  1. origin(我的 git 叉子)
  2. upstream(项目回购)

Does anyone know what is causing my problem?

有谁知道是什么导致了我的问题?

回答by thaller

When doing git submodule update, git tries to checkout the commit/tree which is saved in the super project (in your example, the one with commit id dd208d4...)

这样做时git submodule update,git 会尝试检出保存在超级项目中的提交/树(在您的示例中,具有提交 id 的那个dd208d4...

I think you get the error because within the submodule there no such object present. You have to make sure that it is there. Usually that means that you have to fetch/pull it from a remote first.

我认为您会收到错误消息,因为在子模块中不存在此类对象。你必须确保它在那里。通常这意味着您必须先从远程获取/拉出它。

Probably you have to

可能你必须

git submodule foreach git fetch
git submodule update

or maybe

或者可能

git fetch --recurse-submodules

Assuming, that the submodule is configured, so that it can fetch the missing commit from the remote origin. In the end, youmust know, from where you can fetch the missing commit and you have to get it.

假设子模块已配置,以便它可以从远程获取丢失的提交origin。最后,必须知道,您可以从哪里获取丢失的提交,并且必须获取它。

You could check, whether you have dd208d4...by doing something like:

您可以dd208d4...通过执行以下操作来检查是否有:

cd ./module
git log dd208d46ecdd1ac0d2b2594a610fe4c9150fece1
git cat-file -p dd208d46ecdd1ac0d2b2594a610fe4c9150fece1
git ls-tree dd208d46ecdd1ac0d2b2594a610fe4c9150fece1

One possible cause for such a problem is, that the one who published the new commit from the super module, did not publish the necessary commits from the submodule. He has to publish the commits from the submodule first.

出现这种问题的一个可能原因是,从超级模块发布新提交的人没有从子模块发布必要的提交。他必须首先发布子模块的提交。

回答by protonss

I had the same problem and I resolved adding a new commit for parent project and push allCheck the Subproject commit and commit from your module

我遇到了同样的问题,我解决了为父项目添加新提交并推送所有检查子项目提交并从您的模块提交

回答by Trident D'Gao

just saw this problem when i forgot to push the changes in one of my submodules

当我忘记在我的一个子模块中推送更改时才看到这个问题

make sure changes are pushed

确保推送更改

回答by parasrish

Another way could be, without the command line git options, but to do this manually. The scenario happens mostly, when the sub-module paths have been moved/replaced (but not properly), thereby still pointing to the old references in the locally checkout repositories.

另一种方法是,不使用命令行 git 选项,而是手动执行此操作。这种情况主要发生在子模块路径已被移动/替换(但不正确)时,从而仍指向本地结帐存储库中的旧引用。

1) Locate the repository, and the submodule

1)找到存储库和子模块

ls -la .git/modules
   rm -f .git/modules/<module-with-issue>

2) Remove the older local sub-module configurations

2) 删除旧的本地子模块配置

 gedit .git/config

(remove the sub-module url entry here)

(在此处删除子模块 url 条目)

It looks something like this;

它看起来像这样;

 *[submodule "module-with-issue"]
       url = ...*

3) Now, fetch and update the submodules freshly git fetch git submodule update --recursive --init

3) 现在,重新获取并更新子模块 git fetch git submodule update --recursive --init

Note:One may additionally have to remove, the locally checked-out submodule folder in your repository, before attempting submodule update.

注意:在尝试子模块更新之前,您可能还需要删除存储库中本地检出的子模块文件夹。

回答by c80609a

Adding git rm --cached <submodule-directory>helps me on gitlab:

添加git rm --cached <submodule-directory>对 gitlab 有帮助:

.prepare_deploy: &prepare_deploy
  before_script:
    - bundle install -j $(nproc) --path vendor
    - which ssh-agent || (apt-get update -y && apt-get install openssh-client -y)
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - eval $(ssh-agent -s)
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - (echo "$SSH_PRIVATE_KEY" | base64 --decode) > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - git rm --cached <submodule-directory>
    - git submodule sync --recursive
    - git submodule update --init --recursive

回答by Ben Kreeger

My issue is that upon a cat .gitmodulesof my repo, I was pointing at the wrong remote for the submodule's repo (I had originally cloned it with the original remote, but then switched to a fork of it; the gitmodulesfile never updated to reflect the change).

我的问题是,在cat .gitmodules我的一个 repo 上,我指向了子模块 repo 的错误遥控器(我最初是用原始遥控器克隆它,但后来切换到它的一个分支;该gitmodules文件从未更新以反映更改) .

回答by Rock Lee

My issue was I had uncommitted changes in the submodules in their build.gradle files (which I think were automatically changed). They showed up in the git diff. I just did git checkout .to reset the submodule repos to have no changes and then the git submodule updateworked.

我的问题是我在他们的 build.gradle 文件中的子模块中有未提交的更改(我认为这些更改是自动更改的)。他们出现在git diff. 我只是git checkout .将子模块存储库重置为没有更改,然后就可以git submodule update工作了。