简体   繁体   English

无法签出 git 子模块路径

[英]Unable to checkout git submodule path

I have a problem when working with git submodules.使用 git 子模块时遇到问题。

Whenever i receive a new submodule reference from the upstream repository, executing git submodule update gives 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) origin (我的 git 叉子)
  2. upstream (project repo) upstream (项目回购)

Submodule "module", has remotes:子模块“模块”,有遥控器:

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

Does anyone know what is causing my problem?有谁知道是什么导致了我的问题?

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 . 假设,该子模块被配置,使得它可以读取丢失的从远程提交origin In the end, you must know, from where you can fetch the missing commit and you have to get it. 最后, 必须知道,从哪里可以获取丢失的提交,而您必须获取它。

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. 他必须先从子模块发布提交。

Make sure that the submodules were pushed 确保已推送子模块

cd submodule-dir
git push

In my case, I had: 就我而言,我有:

  • committed to the submodule 致力于子模块
  • not pushed 不推
  • committed to the parent with the updated submodules 使用更新的子模块提交给父级
  • pushed the parent 推父母

so it is no wonder that it could not be found. 因此,难怪找不到它。

Then, if you are using a web interface such as GitHub, you can also go to the submodule repository web page, and double check that the commit you need shows there. 然后,如果您使用的是GitHub之类的Web界面,则还可以转到子模块存储库网页,并仔细检查您所需的提交是否显示在其中。

push.recurseSubmodules on-demand

It is possible to automate pushes further with: 可以使用以下方法进一步自动化推送:

git push --recurse-submodules=on-demand

which also pushes submodules as needed, or starting with 2.7: 这还会根据需要推送子模块,或者从2.7开始:

git config push.recurseSubmodules on-demand
git push

I had the same problem and I resolved adding a new commit for parent project and push all 我有同样的问题,我解决了为父项目添加新提交并推送所有 检查子项目提交并从您的模块中提交

just saw this problem when i forgot to push the changes in one of my submodules 当我忘记在我的一个子模块中推送更改时,刚刚看到了这个问题

make sure changes are pushed 确保更改已推送

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

My issue was I had uncommitted changes in the submodules in their build.gradle files (which I think were automatically changed). 我的问题是我在他们的build.gradle文件中的子模块中有未提交的更改(我认为它们是自动更改的)。 They showed up in the git diff . 他们出现在git diff I just did git checkout . 我只是做了git checkout . to reset the submodule repos to have no changes and then the git submodule update worked. 将子模块存储库重置为没有更改,然后git submodule update

Another way could be, without the command line git options, but to do this manually. 另一种方法可能是,没有命令行git选项,而是手动执行此操作。 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. 当子模块路径已被移动/替换(但不正确)时,大多数情况下会发生这种情况,从而仍然指向本地结帐存储库中的旧引用。

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) (在此处删除子模块网址条目)

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子模块更新--recursive --init

Note: One may additionally have to remove, the locally checked-out submodule folder in your repository, before attempting submodule update. 注意: 在尝试更新子模块之前,可能还必须删除存储库中本地签出的子模块文件夹。

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
git submodule unable to checkout

Note:笔记:

You may need to check if your remote branch is Case Sensitive.您可能需要检查您的远程分支是否区分大小写。 eg: IB_Certs, but you are using ib_certs例如:IB_Certs,但您使用的是 ib_certs

I found that issue, and took a long time to find why, at last, I found it case sensitive.我发现了那个问题,并花了很长时间才找到原因,最后我发现它区分大小写。

Hopefully, this solves your problems.希望这可以解决您的问题。

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

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